欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

In Action HDU - 3339 (最短路+01背包)

程序员文章站 2024-03-17 14:18:07
...

In Action HDU - 3339 (最短路+01背包) 
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe. 
Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to stop it. 
But the arduous task is obviously not easy. First of all, we know that the operating system of the nuclear weapon consists of some connected electric stations, which forms a huge and complex electric network. Every electric station has its power value. To start the nuclear weapon, it must cost half of the electric network's power. So first of all, we need to make more than half of the power diasbled. Our tanks are ready for our action in the base(ID is 0), and we must drive them on the road. As for a electric station, we control them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And we have enough tanks to use. 
Now our commander wants to know the minimal oil cost in this action.

Input

The first line of the input contains a single integer T, specifying the number of testcase in the file. 
For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3...n), and the number of the roads between the station(bi-direction). 
Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between. 
Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station's power by ID order.

Output

The minimal oil cost in this action. 
If not exist print "impossible"(without quotes).

Sample Input

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3

Sample Output

5
impossible

题意:我们需要驾驶坦克毁掉能源站一半以上的能源,每艘坦克只能毁灭一个站点,问如何用最少的油耗销毁一半以上的能源站。

所有的能源站都有销毁和不销毁两种选择,我们可以想到01背包。用dijkstra求出0到各点的最短路,这就是每个物件的耗用,而每个能源站的能量就是物件的价值。我们用0点到所有点的距离之和作为背包的最大值,代码见下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define int long long
#define endl '\n'
#define sc(x) scanf("%lld",&x)

using namespace std;
const int inf=0x3f3f3f3f;
struct Edge{
	int u,v,w;
	Edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){} 
};
struct Node{
	int id,w;
	Node(int id=0,int w=0):id(id),w(w){}
	friend bool operator<(Node a,Node b)
	{
		return a.w>b.w;
	} 
};
int val[105];
int dis[105];
int ans[10005];
vector<Edge> edge[105];
priority_queue<Node> q;
void dijkstra(int s)
{
	dis[s]=0;
	q.push(Node(s,0));
	while(!q.empty())
	{
		Node S=q.top();
		q.pop();
		int id=S.id;
		if(dis[id]!=S.w) continue;
		for(int i=0;i<edge[id].size();i++)
		{
			if(dis[edge[id][i].v]>dis[id]+edge[id][i].w)
			{
				dis[edge[id][i].v]=dis[id]+edge[id][i].w;
				q.push(Node(edge[id][i].v,dis[edge[id][i].v]));
			}
		}
	}
}
void init(int n)
{
	for(int i=0;i<=n;i++) dis[i]=inf;
	for(int i=0;i<=n;i++) edge[i].clear();
	while(!q.empty()) q.pop();
	memset(val,0,sizeof(val));
	memset(ans,0,sizeof(ans));
}
int32_t main()
{
	int n,m;
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		init(n);
		while(m--)
		{
			int u,v,w;
			sc(u),sc(v),sc(w);
			edge[u].push_back(Edge(u,v,w));
			edge[v].push_back(Edge(v,u,w));
		}
		dijkstra(0);
		int sum=0,V=0;
		for(int i=1;i<=n;i++)
		{
			sc(val[i]);
			sum+=val[i];
			if(dis[i]<inf)V+=dis[i];
		}
		sum/=2;
		for(int i=1;i<=n;i++)
		{
			for(int j=V;j>=dis[i];j--)
			{
				ans[j]=max(ans[j],ans[j-dis[i]]+val[i]);
			}
		}
		int flag=0;
		for(int j=0;j<=V;j++)
		{
			if(ans[j]>sum)
			{
				cout<<j<<endl;
				flag=1;
				break;
			}
		}
		if(!flag) cout<<"impossible"<<endl;
	}
	
	return 0;
}