HDU3339 In Action (Dijkstra+01 backpack)

Topic link

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

Title

When the tank travels to the power station, it needs to control at least half of the electricity to prevent the launch of nuclear weapons, and ask about the minimum oil cost to successfully complete the mission.

Problem solving ideas

To find the shortest path, I first thought of using Dijkstra, but the problem has restrictions on electricity, so I chose 01 backpack to judge.

Friendly reminder

1. The size of the dp array is judged (actually I am too
foody , I opened the QAQ too small) 2. When dis==INF, it means that there is no connection, and the data is not included in the total

Code part

```详见代码
#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;
}

Guess you like

Origin blog.csdn.net/WTMDNM_/article/details/107588673