hdoj 3339 In Action 【最短路 + 01背包】

In Action

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


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

 


思路:先求出0点到各点的最短路径,统计总电量sum,sum/2+1是摧毁所需的最小电量,然后是01背包解决。(背包容量===》最短路径的总和;物品===》每条最短路径;价值===》电量值)。


代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f
using namespace std;
int map[110][110];
int vis[110];
int power[110];//电量值; 
int low[110];
int dp[10010];
int n,m;//n个发电站,m条路; 
void init()
{
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			if(i==j)
				map[i][j]=map[j][i]=0;
			else
				map[i][j]=map[j][i]=INF;
		}
	}
}
void dijkstra(int x)
{
	int next,i,j,min;
	memset(vis,0,sizeof(vis));
	for(i=0;i<=n;i++)
	{
		low[i]=map[x][i];
	}
	vis[x]=1;
	for(i=0;i<n;i++)
	{
		min=INF;
		next=0;
		for(j=0;j<=n;j++)
		{
			if(min>low[j]&&!vis[j])
			{
				min=low[j];
				next=j;
			}
		}
		vis[next]=1;
		for(j=0;j<=n;j++)
		{
			if(!vis[j]&&low[j]>low[next]+map[next][j])
			{
				low[j]=low[next]+map[next][j];
			}
		}
	}
}
int main()
{
	int T;
	int i,j;
	int st,ed,dis; 
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		init();
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&st,&ed,&dis);
			if(map[st][ed]>dis)//去重边; 
			map[st][ed]=map[ed][st]=dis;
		}
		int sum=0;
		for(j=1;j<=n;j++)
		{
			scanf("%d",&power[j]);
			sum+=power[j];
		}
		sum/=2;
		dijkstra(0);
		int v=0;
		for(i=1;i<=n;i++)
		{
			if(low[i]!=INF)
			{
				v+=low[i];//背包容量; 
			}
		}
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			if(low[i]!=INF)
			{
				for(j=v;j>=low[i];j--)
				{
					dp[j]=dp[j]>dp[j-low[i]]+power[i]?dp[j]:dp[j-low[i]]+power[i];
				}
			}
		}
		int f=1;
		for(i=0;i<=v;i++)
		{
			if(dp[i]>sum)
			{
				f=0;
				break;
			}
		}
		if(f)
		    printf("impossible\n");
		else
		    printf("%d\n",i);
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值