HDU 3339 In Action (dijkskra+01揹包)

In Action

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


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
 

Author
Lost@HDU
 

Source
HDOJ Monthly Contest – 2010.03.06

題意:這個題意比賽過程一直沒看懂。。(沒有看出一個坦克只能佔領一個地方),看懂很簡單
       就是要用很多坦克去佔領電網,所有坦克都在0位置,有n個電站編號爲1~n,每個電站有它自己的能量值,坦克當且僅當停在上面才能破壞電站(要是再移動了那就不算佔
領),現在要破壞其中一些電站,要讓電網的總能量值損失一半以上,且要使所有坦克去目的地耗費的油量最少。

解題思路:先用Dijkstra算法求出0點到其它任何位置的最短距離,接下來就是0-1揹包問題了,把每條路當作花費,目的地能量值當作價值。注意的是需要破壞一半以上的能量值。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[10010];
int n,m,v[110],map[110][110],d[110],a[110];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0x3f3f,sizeof(map));
        scanf("%d %d",&n,&m);
        int s,e,load;
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&s,&e,&load);
            map[s][e]=map[e][s]=min(map[s][e],load);
        }
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(v,0,sizeof(v));
        for(int i=0;i<=n;i++) d[i]=(i==0 ? 0 : INF);
        for(int i=0;i<=n;i++){ //Dijkstra模板求固定點到任意點的最短路
            int x,ans=INF;
            for(int y=0;y<=n;y++) if(!v[y] && d[y]<=ans) ans=d[x=y];
            //cout << 1 << endl;
            v[x]=1;
            for(int y=0;y<=n;y++) d[y]=min(d[y],d[x]+map[x][y]);
            //cout << 2 << endl;
        }
        int load_sum=0,sum=0;
        for(int i=1;i<=n;i++)
            sum+=a[i];
        sum=sum/2+1; // 能量要超過一半以上,所以要加一
        for(int i=1;i<=n;i++)
            if(d[i]!=INF)
                load_sum+=d[i];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++) // 進行0-1揹包
        {
            if(d[i]==INF) continue; // 如果路不存在,跳過
            for(int j=load_sum;j>=d[i];j--){
                 dp[j]=max(dp[j],dp[j-d[i]]+a[i]);
            }
        }
        int res=-1;
        for(int i=0;i<=load_sum;i++)
          if(dp[i]>=sum){ // 找到第一個滿足條件的,就是需要最少的花費(也就是耗油量)
             res=i;
             break;
          }
        if(res==-1) printf("impossible\n");
        else printf("%d\n",res);
    }
    return 0;
}</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章