程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 3339 In Action(最短路+背包)

HDU 3339 In Action(最短路+背包)

編輯:C++入門知識


題目:
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
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個電站編號為1~n,每個電站有它自己的能量值。有一個軍事基地編號為0,裡面有無限個坦克,可以開到某個電站轟炸破壞掉電站,並且一個坦克只能破壞一個。現在要破壞掉其中一些電站,要讓電網的總能量值損失一半以上, 並且要讓所有執行任務的坦克去目的地路費最少。

分析與總結:
1. 容易想到用單源最短路算法求出0到所有其他點的最短距離。
2. 關鍵是要選擇破壞哪些電站,使得這些破壞的電站總能量值為原來的一半以上。
    因為對於每一個電站,要麼是選擇破壞,要麼是不破壞,那麼便可以聯想到經典的01背包問題。
    把求得的最短距離總和當作背包容量, 各個站的能量值當作物品價值,然後OK。

 

代碼:
[cpp] 
#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; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved