HDU3339 In Action(Dijkstra+01背包)

题目链接

In Action

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

题意

坦克行进至发电站,至少需要控制一半以上的电力以阻止核武器发射,询问成功完成任务时需要耗费的最小石油成本。

解题思路

要求寻找最短路,首先想到使用Dijkstra,但题目对于电力存在限制,选择01背包进行判断。

友情提醒

1.dp数组的大小判断(其实是我太菜,开小了QAQ)
2.dis==INF的时候,表示不连通,数据不计入总和

代码部分

```详见代码
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
bool vis[105];
int n,m,t,d,st,ed,sum,res,power[105],a[105][105],dp[10005],dis[105];
void dij()
{
	for(int i=0;i<105;i++)
	{
		dis[i]=inf;
		vis[i]=false;	
	}//初始化
	dis[0]=0;
	for(int i=0;i<n;i++)
	{
		int u=-1,Min=inf;
		for(int j=0;j<=n;j++)
		{
			if(!vis[j]&&dis[j]<Min)
			{
				u=j;
				Min=dis[j];
			}
		}//选取最近的点 
		if(u==-1)
			return ;
		vis[u]=true;
		for(int k=0;k<=n;k++)
		{
			if(!vis[k]&&a[u][k]!=inf&&dis[u]+a[u][k]<dis[k])
			{
				dis[k]=dis[u]+a[u][k];
			}
		}//松弛,更新最短路
	}
} 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>t;
	while(t--)
	{
		for(int i=0;i<105;i++)
			for(int j=0;j<105;j++)
				a[i][j]=inf;
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>st>>ed>>d;
			a[st][ed]=a[ed][st]=min(d,a[st][ed]);
		}//注意同样的路径也许存在多组数据,需要选取距离最近的计入 
		dij();
		sum=res=0;
		for(int i=1;i<=n;i++)
		{
			cin>>power[i];
			res+=power[i];
			if(dis[i]!=inf)//判断筛除无法到达的 
				sum+=dis[i];
		}//记录电力总和,同时统计可到达的发电站距离和作为背包容量 
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			for(int j=sum;j>=dis[i];j--)
			{
				dp[j]=max(dp[j],dp[j-dis[i]]+power[i]);
			}
		}//一定距离内可控制的最大电力
		int flag=1;
		for(int i=1;i<=sum;i++)
		{
			if(dp[i]>=res/2+1)//可控制电力大于一半符合要求 
			{
				flag=0;
				cout<<i<<endl;//输出所需最小路程即耗油量 
				break;
			}
		}
		if(flag)
			cout<<"impossible"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值