HDU3339 In Action 【SPFA】+【01背包】

来源:互联网 发布:mac照片icloud 编辑:程序博客网 时间:2024/06/05 17:00

In Action

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


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.
 

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 

题意:开始没整明白题意,题意是在0点有很多坦克,指派一些坦克到一些点去,一个坦克只能控制一个点,问怎样能控制一半以上的能量并使总路径最短。
题解:用SPFA求出所有点到0点的最短距离,然后以总路径为背包,点的能量值为价值,对每个点只有去和不去两种状态,用01背包解决。

#include <stdio.h>#include <string.h>#include <queue>#define inf 0x3f3f3f3f#define maxn 102#define maxm 10002using std::queue;int head[maxn], id, dist[maxn];int pow[maxn], dp[100 * maxn];struct Node{int to, cost, next;} E[maxm << 1];bool vis[maxn];int max(int a, int b){return a > b ? a : b;}void addEdge(int u, int v, int cost){E[id].to = v; E[id].cost = cost;E[id].next = head[u]; head[u] = id++;E[id].to = u; E[id].cost = cost;E[id].next = head[v]; head[v] = id++;}void SPFA(int s, int n, int m){int i, u, v, tmp;for(i = 0; i <= n; ++i){vis[i] = false; dist[i] = inf;}u = s; vis[u] = true; dist[u] = 0;queue<int> Q;Q.push(u);while(!Q.empty()){u = Q.front(); Q.pop();vis[u] = false;for(i = head[u]; i != -1; i = E[i].next){tmp = dist[u] + E[i].cost;v = E[i].to;if(tmp < dist[v]){dist[v] = tmp;if(!vis[v]){vis[v] = true;Q.push(v);}}}}}void zeroOnePack(int n, int sumDist){int i, j;memset(dp, 0, sizeof(int) * (sumDist + 1));for(i = 1; i <= n; ++i)for(j = sumDist; j >= dist[i]; --j){dp[j] = max(dp[j], dp[j-dist[i]] + pow[i]);}}int main(){int n, m, u, v, cost, i, t, sumDist, sumCost;scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);memset(head, -1, sizeof(int) * (n + 1));for(i = id = 0; i < m; ++i){scanf("%d%d%d", &u, &v, &cost);addEdge(u, v, cost);}for(i = 1, sumCost = 0; i <= n; ++i){scanf("%d", &pow[i]);sumCost += pow[i];}SPFA(0, n, id);for(i = sumDist = 0; i <= n; ++i)if(dist[i] != inf) sumDist += dist[i];zeroOnePack(n, sumDist);sumCost >>= 1;cost = -1;for(i = 1; i <= sumDist; ++i)if(dp[i] > sumCost){cost = i; printf("%d\n", cost);break;}if(cost == -1) printf("impossible\n");}return 0;}


0 0