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

In Action

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

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个发电站,m条路,每条路有各自的距离,每个发电站有各自的发电量,现在需要炸毁它们,一辆坦克只能炸毁一个发电站,而且需要炸毁的发电厂的发电量需要大于所有发电站所产生的总电量的一半,求坦克走的最短距离。
因为题目说明不超过100个点,所以可以用Floyd算法,把所有的0到各个发电站的距离找出来,然后把0能到的各个发电站的距离相加作为背包的容量,以dp数组储存的值作为炸毁的发电站的电量,最后进行判断,如果dp[i]的值大于所有发电站的总电量,输出i的值,此时i的值就是坦克炸毁发电站使城市供电瘫痪所能走的最短距离

 1 #include <iostream>
 2 #include <cstring>
 3 #define N 110
 4 #define inf 0X3f3f3f3f
 5 using namespace std;
 6 int mapp[N][N];
 7 int n,m;
 8 int dp[100010];
 9 int oil[10010];
10 void floyd(){
11     for(int k=0;k<=n;k++){
12         for(int i=0;i<=n;i++){
13             for(int j=0;j<=n;j++){
14                 mapp[i][j]=min(mapp[i][j],mapp[i][k]+mapp[k][j]);
15             }
16         }
17     }
18 }
19 int main(){
20     cin.sync_with_stdio(false);
21     int T,sum,cnt;
22     cin>>T;
23     while(T--){
24         sum=0;
25         cnt=0;
26         cin>>n>>m;
27         for(int i=0;i<=n;i++){
28             for(int j=0;j<=n;j++){
29                 mapp[i][j]=inf;
30             }
31             mapp[i][i]=0;
32         }
33         for(int i=0;i<m;i++){
34             int a,b,c;
35             cin>>a>>b>>c;
36             mapp[b][a]=mapp[a][b]=min(mapp[a][b],c);
37         }
38         floyd();
39         for(int i=1;i<=n;i++){
40             cin>>oil[i];
41             sum+=oil[i];
42             if(mapp[0][i]!=inf){
43                 cnt+=mapp[0][i];
44             }
45         }
46         memset(dp,0,sizeof(dp));
47         for(int i=1;i<=n;i++){
48             for(int j=cnt;j>=mapp[0][i];j--){
49                 if(dp[j-mapp[0][i]]+oil[i]>dp[j]){
50                     dp[j]=dp[j-mapp[0][i]]+oil[i];//坦克走j的长度最多炸多少电量
51                 }
52             }
53         }
54         int flag = 0;
55         for(int i=1;i<=cnt;i++){
56             if(dp[i]>sum*1.0/2){
57                 flag=1;
58                 cout<<i<<endl;
59                 break;
60             }
61         }
62         if(!flag){
63             cout<<"impossible"<<endl;
64         }
65     }
66     return 0;
67 }

2016-12-18 17:08:02

posted @ 2016-12-18 17:08  ガ落涙『不變』  阅读(99)  评论(0编辑  收藏  举报