HDU 3339 in Action (Shortest Path + backpack)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3339

Question:

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 into ss 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 plan. Now, we need to stop it.
But the arduous task is obviusly 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.
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

22 30 2 92 1 31 0 2132 12 1 313
 


Sample output

5impossible
 
Question:To destroy a power grid, there are N power stations numbered 1 ~ N. Each power station has its own energy value. There is a military base numbered 0, which contains an infinite number of tanks. It can be used to bomb and destroy a power station at a power station, and only one tank can destroy one. It is necessary to destroy some of these power stations, so that the total capacity of the power grid will be lost by more than half, and the transportation cost for all tanks running the task will be the least.
Analysis and Summary:1. It is easy to use the single-source shortest path algorithm to find the shortest distance from 0 to all other points. 2. The key is to select which power stations are damaged, so that the total number of damaged power stations is more than half of the original. For each power station, either damage or damage, you can think of the classic 01 backpack problem. Take the obtained shortest distance sum as the backpack capacity, the energy value of each station as the item value, and then OK. Code:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<utility>using namespace std;typedef pair<int, int>pii;const int INF = 0x7fffffff;const int VN = 105;const int EN = 20005;struct Edge{int v,next,w;}E[EN];priority_queue<pii,vector<pii>,greater<pii> >q;int n, size;int head[VN], d[VN], pow[VN];int dp[10005];int oil[10005];void init(){    size = 0;    memset(head, -1, sizeof(head));    while(!q.empty()) q.pop();}void addEdge(int u,int v,int w){    E[size].v=v, E[size].w=w;    E[size].next = head[u];    head[u] = size++;}void Dijkstra(int src){    for(int i=0; i<=n; ++i) d[i] = INF;    d[src] = 0;    q.push(make_pair(d[src], src));    while(!q.empty()){        pii x = q.top();  q.pop();        int u = x.second;        if(d[u] != x.first) continue;        for(int e=head[u]; e!=-1; e=E[e].next){            int tmp = d[u] + E[e].w;            if(d[E[e].v] > tmp){                d[E[e].v] = tmp;                q.push(make_pair(tmp,E[e].v));            }        }    }} int main(){    int T,m,u,v,c;    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&m);        init();        for(int i=0; i<m; ++i){            scanf("%d%d%d",&u,&v,&c);            addEdge(u,v,c);            addEdge(v,u,c);        }        for(int i=1; i<=n; ++i){            scanf("%d",&pow[i]);        }        Dijkstra(0);        if(d[1]==INF){            puts("impossible"); continue;        }        int dis_sum = 0, pow_sum = 0;        for(int i=1; i<=n; ++i){            dis_sum += d[i];            pow_sum += pow[i];        }        memset(dp, 0, sizeof(dp));        for(int i=1; i<=n; ++i)            for(int j=dis_sum; j>=d[i]; --j)                dp[j] = max(dp[j],dp[j-d[i]]+pow[i]);        pow_sum = (pow_sum>>1)+1;        for(int i=1; i<=dis_sum; ++i){            if(dp[i]>=pow_sum){                printf("%d\n", i);                break;            }        }    }     return 0;}
 
  

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.