欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 3339 In Action (0-1背包+最短路)

程序员文章站 2024-03-17 14:18:19
...

传送门:点击打开链接

In Action

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


Problem Description
HDU 3339 In Action (0-1背包+最短路)
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个点和m条边连接两个点,每个点有一个功率,现在在位置0有足够的坦克,问你最小的花费使得坦克占领的核电站额功率和大于总的功率的一半。

先用最短路搜到从0到所有核电站的最短路,此时得到所有最短路,但还有要求要功率大于总功率的一半,换句话说就是权值已经确定,每经过一点需要规定花费,且花费最小,此时就是典型的0-1背包的思想,dp[i]表示权值为i时的最小花费,最后枚举花费,一旦大于总功率的一半,跳出循环,输出此时的花费。
由于数据量很小,可以用spfa,floyd,dijkstra来完成,说了数据量很小,但spfa使用cin,cout输入输出仍会超时。

代码实现:

floyd

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=105;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int map[maxn][maxn],dp[maxn*maxn*2],value[maxn*maxn];
int m,n;

void floyd()
{
	int i,j,k;
	for(i=0;i<=n;i++)
	{
		for(j=0;j<=n;j++)
		{
			for(k=0;k<=n;k++)
			{
				if(map[j][k]>map[j][i]+map[i][k])
				{
					map[j][k]=map[j][i]+map[i][k];
				}
			}
		}
	}
}

int solve()
{
	int i,j,k,sum=0,count=0;
	for(i=1;i<=n;i++)
	{
		cin>>value[i];
		sum+=value[i];
		
		if(map[0][i]!=inf)
		count+=map[0][i];	
	}
	
	for(i=0;i<=n;i++)
	{
		for(j=count;j>=map[0][i];j--)
		{
			dp[j]=max(dp[j],dp[j-map[0][i]]+value[i]);
		}
	}
	
	for(i=1;i<=count;i++)
	{
		if(dp[i]>sum/2)
		{
			return i;
		}
	}
	
	return -1;
}

int main()
{
	int x,y,z,i,j,k,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				map[i][j]=inf;
				
				if(i==j)
				map[i][j]=0;
			}
		}
		mset(dp,0);
		for(i=0;i<m;i++)
		{
			cin>>x>>y>>z;
			if(map[x][y]>z)
			map[x][y]=map[y][x]=z;
		}
		floyd();
		int ans=solve();
		
		if(ans!=-1)
		cout<<ans<<endl;
		else
		cout<<"impossible"<<endl;
	}
	return 0;
}


dijkstra

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=105;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int map[maxn][maxn],dp[maxn*maxn*2],value[maxn*maxn],visit[maxn],cast[maxn];
int m,n;

void dijkstra()
{
	int i,j,pos,minn;
	mset(visit,0);            //清空标记数组 
	visit[0]=1;               //初始点0被标记 
	for(i=0;i<=n;i++)          //记录最短路 
	cast[i]=map[0][i];
	
	for(i=1;i<=n;i++)         //枚举每一个点 
	{
		minn=inf;             //minn初始化 
		for(j=1;j<=n;j++)     //枚举 
		{
			if(cast[j]<minn&&!visit[j]) //枚举每一个未被标记过的点 
			{
				pos=j;         //记录最短路存在的节点 
				minn=cast[j];  //记录最短路 
			}
		}
		visit[pos]=1;          //标记当前最短路的节点 
		for(j=1;j<=n;j++)      //枚举每一个点 
		{
			if(cast[pos]+map[pos][j]<cast[j]&&!visit[j]) //储存每个未被标记点当加上此点到达最短路的值 
			cast[j]=cast[pos]+map[pos][j];
		}
	}
}

int solve()
{
	int i,j,k,sum=0,count=0;
	for(i=1;i<=n;i++)
	{
		cin>>value[i];
		sum+=value[i];
		
		if(map[0][i]!=inf)
		count+=map[0][i];	
	}
	
	for(i=0;i<=n;i++)
	{
		for(j=count;j>=cast[i];j--)
		{
			dp[j]=max(dp[j],dp[j-cast[i]]+value[i]);
		}
	}
	
	for(i=1;i<=count;i++)
	{
		if(dp[i]>sum/2)
		{
			return i;
		}
	}
	
	return -1;
}

int main()
{
	int x,y,z,i,j,k,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				map[i][j]=inf;
				
				if(i==j)
				map[i][j]=0;
			}
		}
		mset(dp,0);
		for(i=0;i<m;i++)
		{
			cin>>x>>y>>z;
			if(map[x][y]>z)
			map[x][y]=map[y][x]=z;
		}
		dijkstra();
		int ans=solve();
		
		if(ans!=-1)
		cout<<ans<<endl;
		else
		cout<<"impossible"<<endl;
	}
	return 0;
}


spfa

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=105;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct edge{
	int v,w,next;
}p[maxn*2*maxn];

int head[maxn*maxn],dis[maxn*maxn],map[maxn][maxn],value[maxn],dp[maxn*maxn],visit[maxn];
int m,n,cnt;

void init()
{
	mset(dp,0);
	mset(head,-1);
	mset(visit,0);
	mset(dis,inf);
}

void add(int u,int v,int w)
{
	p[cnt].v=v;
	p[cnt].w=w;
	p[cnt].next=head[u];
	head[u]=cnt++;
}

void spfa(int pos)
{
	int i,j,k;
	dis[pos]=0;
	visit[pos]=1;
	queue <int> q;
	q.push(pos);
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		visit[temp]=0;
		for(i=head[temp];i!=-1;i=p[i].next)
		{
			int count=p[i].v;
			if(dis[count]>dis[temp]+p[i].w)
			{
				dis[count]=dis[temp]+p[i].w;
				if(!visit[count])
				{
					visit[count]=1;
					q.push(count);
				}
			}
		}
	}
}

int main()
{
	int t,i,j,k,count,sum,x,y,z;
	cin>>t;
	while(t--)
	{
		init();
		cnt=0;
		count=sum=0;
		
		cin>>n>>m;
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
			add(y,x,z);
		}
		
		for(i=1;i<=n;i++)
		{
			scanf("%d",&value[i]);
			sum+=value[i];
		}
		
		spfa(0);
		for(i=1;i<=n;i++)
		if(dis[i]!=inf)
		count+=dis[i];
		
		for(i=0;i<=n;i++)
		{
			for(j=count;j>=dis[i];j--)
			{
				dp[j]=max(dp[j],dp[j-dis[i]]+value[i]);
			}
		}
		int l,flag=0;
		for(l=0;l<=count;l++)
		{
			if(dp[l]>sum/2)
			{
				flag=l;
				break;
			}
		}
		if(flag)
		cout<<flag<<endl;
		else
		cout<<"impossible"<<endl;
	}
	return 0;
}