HDU 3339 In Action

来源:互联网 发布:伤害跳跃网络吧 编辑:程序博客网 时间:2024/05/09 01:44
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): 4325    Accepted Submission(s): 1395


Problem Description
HDU 3339 In Action - 风未定 - NGUNAUJ
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
22 30 2 92 1 31 0 2132 12 1 313
 

Sample Output
5impossible
 

Author
Lost@HDU
 

Source
HDOJ Monthly Contest – 2010.03.06
最短路+01背包,,复习了01背包。。真的是个不错的题目从此最短路不在寂寞
注意:这道题目是有重边的(无向)都可以走。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<20
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
int n,m;
int v;
int a,b,c;
int edge[110][110];
int sum;
int vis[110];int lowcost[110];
int dp[10011];
int val[110];
void dj()
{
cle(vis);
vis[0]=1;
for(int i=1;i<=n;i++)
lowcost[i]=edge[0][i];
int Min,k;
while(1)
{
Min=INF;
for(int i=1;i<=n;i++)
if(lowcost[i]<Min&&!vis[i])
{
Min=lowcost[i];k=i;
}
if(Min==INF)break;
vis[k]=1;
for(int j=1;j<=n;j++)
if(lowcost[k]+edge[k][j]<lowcost[j]&&!vis[j])
lowcost[j]=lowcost[k]+edge[k][j];
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--)
{
sum=0;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
edge[i][j]=INF;
edge[i][i]=0;
}

for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(edge[a][b]>c)
{
edge[a][b]=c;edge[b][a]=c;
}
}
for(int i=1;i<=n;i++)
{
cin>>val[i];
sum+=val[i];
}
dj();
v=0;int mark=0;
for(int i=1;i<=n;i++)
{
v+=lowcost[i];
if(lowcost[1]==INF)
{
mark=1;break;
}
}
cle(dp); ///把总费用当作体积 价值为val[] dp[j]为在体积小于等于 V 能达到的最大价值
if(mark)printf("impossible\n");
else
{
for(int i=1;i<=n;i++)
for(int j=v;j>=lowcost[i];j--)
dp[j]=max(dp[j],dp[j-lowcost[i]]+val[i]);
for(int i=1;i<=v;i++)
if(dp[i]>=sum/2+1){cout<<i<<endl;break;}
}
//system("pause");
}
return 0;
}


hdu-3339In Action - 风未定 - Guanjun的博客~~
 
luanhua的帮助理解
0 0
原创粉丝点击