In Action

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

题目大意:给定N个电站和M条双向道路,现在坦克在0号电站,要去炸毁这些电站,问在路径最短的情况下能不能炸毁超过所有电站发电量一半的电量?

思路:因为电站数量较少,先考虑用弗洛伊德算法求出最短路径,然后把路径看着每个电站的体积,发电量看做电站的价值,就变成了01背包问题,最后只要判断能否炸毁超过一半电量就可以。

code:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int map[305][305]; int dp[100010]; int a[10010]; int n,m; void flod() { for(int k=0;k<=n;k++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) if(map[i][j] > map[i][k]+map[k][j]) map[i][j] = map[i][k] + map[k][j]; } int main() { int t; scanf("%d",&t); while(t--) { int x,y,z; scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) { map[i][j] = INF; if(i==j) map[i][j] = 0; } for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); if(map[x][y] > z) { map[x][y] = z; map[y][x] = z; } } flod(); int sum = 0; int dis = 0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum += a[i]; if(map[0][i] != INF) dis += map[0][i]; } memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=dis;j>=map[0][i];j--) if(dp[j-map[0][i]] + a[i] > dp[j]) dp[j] = dp[j-map[0][i]] + a[i]; int flag = 0; for(int i=1;i<=dis;i++) if(dp[i] > sum / 2) { flag = 1; printf("%d\n",i); break; } if(!flag) printf("impossible\n"); } return 0; }