HDU3339 In Action(最短路+01背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3339


In Action

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


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

题意:有人想要用核弹毁灭地球。现在有n个发电站(1-n),每个发电站有自己的发电量,需要控制一些发电站,使总发电量小于一半。

控制发电站的方法是让一辆坦克停在发电站里。开一单位需要一单位油。我们有足够的坦克,问最少需要多少油才能完成任务,不能则输出impossible。


题目不难,不过容易看错题。。

1.有很多坦克,都是从0出发,控制发电站后坦克不能再移动,需要再派出一辆坦克从0出发。

2.超过一半的发电量,不管奇数偶数,都要大于 整除2。

3.有些发电站无法到达并不意味着任务不能完成,可能已经控制了超过一半的电量。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f

int n,m,posum;
int g[105][105];
int po[105],dis[105],vis[105],pre[105];
int dp[1000005]; 

void dijkstra(int s)//求出到所有点的最短路
{
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(pre,-1,sizeof(pre));
	dis[s]=0;vis[s]=1;
	for (int i=0;i<=n;i++)
	{
		for (int j=0;j<=n;j++)
		{
			if (g[s][j]!=0)
			{
				if (dis[j]>dis[s]+g[s][j])
				{
					dis[j]=dis[s]+g[s][j];
					pre[j]=s;
				}
			}
		}
		for (int j=0;j<=n;j++)
		{
			if (vis[j]==0&&(vis[s]==1||dis[s]>dis[j])) s=j;
		}
		vis[s]=1;
	}
}

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		memset(g,0,sizeof(g));
		for (int i=1;i<=m;i++)
		{
			int u,v,c;
			scanf("%d%d%d",&u,&v,&c);
			if (g[u][v]==0)
			{
				g[u][v]=c;g[v][u]=c;
			}
			else
			{
				if (g[u][v]>c)
				{
					g[u][v]=c;g[v][u]=c;
				}
			}
		}
		posum=0;
		for (int i=1;i<=n;i++)
		{
			scanf("%d",&po[i]);
			posum+=po[i];
		}
		posum/=2;//总电量的一半
		dijkstra(0);
		int mapo=0,madis=0;//求出能够到达的发电站的总最短路与总电量
		for (int i=1;i<=n;i++)
		{
			if (dis[i]!=INF)
			{
				madis+=dis[i];
				mapo+=po[i];
			}
		}
		if (mapo<=posum) {printf("impossible\n");continue;}//总电量没超过一半,输出impossible
		memset(dp,0,sizeof(dp));//01背包,最短路为代价,发电量为价值,最大体积为总最短路
		for (int i=1;i<=n;i++)
		{
			for (int j=madis;j>=dis[i];--j)
			{
				dp[j]=max(dp[j],dp[j-dis[i]]+po[i]);
			}
		}
		int ans=0;
		for (int i=0;i<=madis;i++)//从小到大,第一个满足条件的即为答案
		{
			if (dp[i]>posum)
			{
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值