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.
InputThe 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.OutputThe 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
题意:一些坦克要占据一些能量据点,坦克从0点出发,总共有编号1-n n个能量据点,如果要摧毁敌方,必须要占领能量据点的能量值达到总能量的一半以上,现在知道m条路径,以及坦克在m条路上的油耗,然后知道每个能量据点的能量值,问摧毁敌方所需的最少油耗.
题解:最短路+01背包,将每个能量据点看成背包容量,油耗看成价值,然后进行01背包求解
AC代码为:
#include<bits/stdc++.h> using namespace std; const int N = 105; const int INF = 99999999; int graph[N][N]; int low[N]; bool vis[N]; int w[N]; int dp[N*N]; int n,m; int dijkstra(int s) { for(int i=1;i<=n;i++) { low[i] = graph[s][i]; vis[i] = false; } low[s] = 0; vis[s] = true; for(int i=1;i<n;i++) { int Min = INF; for(int j=1;j<=n;j++) { if(Min>low[j]&&!vis[j]) { Min = low[j]; s = j; } } vis[s] = true; for(int j=1;j<=n;j++) { if(low[j]>low[s]+graph[s][j]&&!vis[j]) { low[j] = low[s]+graph[s][j]; } } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { if(i==j) graph[i][j] = 0; else graph[i][j] = INF; } } for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c<graph[a][b]) graph[a][b]=graph[b][a] =c; } int sum = 0; for(int i=1;i<=n;i++) { scanf("%d",&w[i]); sum+=w[i]; } dijkstra(0); for(int i=1;i<=sum;i++) dp[i] = INF; dp[0] = 0; for(int i=1;i<=n;i++) { for(int v = sum;v>=w[i];v--) dp[v] = min(dp[v],dp[v-w[i]]+low[i]); } int sum1 = sum/2+1; int Min = INF; for(int i=sum1;i<=sum;i++) if(dp[i]<Min) Min = dp[i]; if(Min==INF) printf("impossible "); else printf("%d ",Min); } }