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

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4859    Accepted Submission(s): 1600


Problem Description

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
 

Author
Lost@HDU
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1217 1385 1548 3338 1546 

n个电站,m条路,点0处出发,每一个电站都有一定的发电量p [ i ],也给出了m条路,a--b需要c的油,如果成功的控制一半以上的电量就视为胜利,现在求最少的耗油量,先用迪杰斯特拉处理路径,背包中存i升的油最多控制j的电量(dp[ i ] = j)然后找出最小的dp[ i ] > sum/2,输出i

ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 110
int map[MAXN][MAXN],vis[MAXN],dis[MAXN],dp[11000],p[MAXN];
int n,m;
void disjtra()
{
	memset(vis,0,sizeof(vis));
	memset(dis,INF,sizeof(dis));
	for(int i=0;i<=n;i++)
	dis[i]=map[0][i];
	vis[0]=1;
	for(int i=1;i<=n;i++)
	{
		int minn=INF;
		int flag=0;
		for(int j=0;j<=n;j++)
		{
			if(!vis[j]&&minn>dis[j])
			{
				minn=dis[j];
				flag=j;
			}
		}
		vis[flag]=1;
		for(int j=0;j<=n;j++)
		{
			if(!vis[j]&&dis[flag]+map[flag][j]<dis[j])
			{
				dis[j]=dis[flag]+map[flag][j];
			}
		}
	}
}
int main()
{
	int t,i,j;
	int a,b,c;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		memset(map,INF,sizeof(map));
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(c<map[a][b])
				map[a][b]=map[b][a]=c;
		}
		int sum=0;
		for(i=1;i<=n;i++) 
		{
			scanf("%d",&p[i]);
			sum+=p[i];
		}
		sum/=2;
		disjtra();
		int v=0;
		for(i=1;i<=n;i++)
		{
			if(dis[i]!=INF)
				v+=dis[i];
		}
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			if(dis[i]!=INF)
			{
				for(j=v;j>=dis[i];j--)
					dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
			}
		}
		int flag=1;
		for(i=0;i<=v;i++) 
		{
			if(dp[i]>sum)
			{
				flag=0;
				break;
			}
		}
		if(flag)
			printf("impossible
");
		else
			printf("%d
",i);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/playboy307/p/5273498.html