In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2157 Accepted Submission(s): 712
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.
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).
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
1 /* 2 思路:先用dijkstra求出基地(0点)到各个发电站的最短路径,统计出电量之和P, 3 则P/2+1就是需要摧毁的最少电量值。然后进行01背包, 4 背包容量是从基地到各个发电站的最短路径的总和,物品就是每条最短路径, 5 其容量为路径长度,价值为该路径终点发电站的电量值。 6 然后即可求出满足给定价值的最小容量,如果均不满足,则输出“impossible”。 7 代码如下: 8 */ 9 #include<stdio.h> 10 #include<string.h> 11 #define inf 0x3fffffff 12 13 int map[101][101]; 14 int power[101]; 15 int dis[101]; 16 int dp[10001]; 17 18 void dijks(int n) 19 { 20 int i,j,k,min; 21 int visit[101]={0}; 22 for(i=0;i<=n;++i) 23 dis[i]=map[0][i]; 24 visit[0]=1; 25 for(i=1;i<=n;++i) 26 { 27 min=inf; 28 k=0; 29 for(j=0;j<=n;++j) 30 { 31 if(!visit[j]&&min>dis[j]) 32 { 33 min=dis[j]; 34 k=j; 35 } 36 } 37 visit[k]=1; 38 for(j=0;j<=n;++j) 39 { 40 if(!visit[j]&&dis[k]+map[k][j]<dis[j]) 41 dis[j]=dis[k]+map[k][j]; 42 } 43 } 44 } 45 46 int main() 47 { 48 int T,n,m,sum,flag,i,j,st,ed,len,v; 49 scanf("%d",&T); 50 while(T--) 51 { 52 53 scanf("%d%d",&n,&m);//n:表示n个发电站 m:表示m条边 54 for(i=0;i<=n;++i) 55 for(j=0;j<i;++j) 56 map[i][j]=map[j][i]=inf; 57 for(i=0;i<m;++i) 58 { 59 scanf("%d%d%d",&st,&ed,&len); 60 if(len<map[st][ed]) //有重边 61 map[st][ed]=map[ed][st]=len; 62 } 63 sum=0; 64 for(i=1;i<=n;++i) 65 { 66 scanf("%d",&power[i]); 67 sum+=power[i]; 68 } 69 sum>>=1; 70 dijks(n); 71 v=0; 72 for(i=1;i<=n;++i) 73 { 74 if(dis[i]!=inf) 75 v+=dis[i]; //距离总和作为背包容量 76 } 77 memset(dp,0,sizeof(dp)); //注意0-1背包初始化都为0 78 for(i=1;i<=n;++i) 79 if(dis[i]!=inf) 80 for(j=v;j>=dis[i];--j) 81 dp[j]=dp[j]>dp[j-dis[i]]+power[i]?dp[j]:dp[j-dis[i]]+power[i]; 82 flag=1; 83 for(i=0;i<=v;++i) 84 { 85 if(dp[i]>sum) 86 { 87 flag=0; 88 break; 89 } 90 } 91 if(flag) 92 printf("impossible\n"); 93 else 94 printf("%d\n",i); 95 } 96 return 0; 97 }