码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-26 22:08:36      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

In Action

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


Problem Description
bubuko.com,布布扣
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
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3338 1546 3333 3341 3335 
 

 

bubuko.com,布布扣
 1 //187MS    616K    1856 B    C++
 2 /*
 3 
 4     题意:
 5         题意挺重要的。一个源点0,源点每到一个站可得到一个pow[i],求得到总pow值一半以上时最短行走距离。
 6         
 7     最短路+01背包:
 8         首先最短路求出源点0到每一个点的距离,然后得出n个点,每个点有一个距离花费d,和一个价值权值w,
 9     以到所有的点最短路程和作为背包容量,距离作为花费,权值作为价值,01背包。
10     当dp[j]>sum_pow/2+1时的距离和j为所求。  
11 
12 */
13 #include<iostream>
14 #include<queue>
15 #include<vector>
16 #define N 105
17 #define inf 0x7ffffff
18 using namespace std;
19 typedef struct node{
20     int d,w;
21 }node;
22 node p[N];
23 int vis[N];
24 int d[N];
25 int dp[N*N];
26 vector<node>V[N];
27 int n;
28 int Max(int x,int y)
29 {
30     return x>y?x:y;
31 }
32 void spfa(int s)
33 {
34     for(int i=0;i<=n;i++)
35         d[i]=inf;
36     d[s]=0;
37     vis[s]=1;
38     queue<int>Q;
39     Q.push(s);
40     while(!Q.empty()){
41         int u=Q.front();
42         Q.pop();
43         vis[u]=0;
44         int m=V[u].size();
45         for(int i=0;i<m;i++){
46             int v=V[u][i].d;
47             int w=V[u][i].w;
48             if(d[v]>d[u]+w){
49                 d[v]=d[u]+w;
50                 if(!vis[v]){
51                     vis[v]=1;
52                     Q.push(v);
53                 }
54             } 
55         }
56     }
57 } 
58 int main(void)
59 {
60     int t,m;
61     int u,v,w,pow;
62     scanf("%d",&t);
63     while(t--)
64     {
65         int sd=0,sw=0;
66         scanf("%d%d",&n,&m);
67         for(int i=0;i<=n;i++) V[i].clear();
68         for(int i=0;i<m;i++){
69             scanf("%d%d%d",&u,&v,&w); 
70             node q={v,w};
71             V[u].push_back(q);
72             q.d=u;
73             V[v].push_back(q);
74         }
75         for(int i=1;i<=n;i++){
76             scanf("%d",&p[i].w);
77             sw+=p[i].w;
78         }
79         spfa(0);
80         for(int i=1;i<=n;i++){
81             p[i].d=d[i];
82             if(d[i]!=inf) 
83                 sd+=p[i].d;
84         }
85         memset(dp,0,sizeof(dp));
86         for(int i=1;i<=n;i++)
87             for(int j=sd;j>=p[i].d;j--)
88                 dp[j]=Max(dp[j],dp[j-p[i].d]+p[i].w);
89         int ans=0;
90         for(int i=1;i<=sd;i++)
91             if(dp[i]>=sw/2+1){ 
92                 ans=i;break;
93             }
94         if(ans==0) puts("impossible");
95         else printf("%d\n",ans);
96     }
97     return 0;
98 } 
bubuko.com,布布扣

 

 

hdu 3339 In Action (最短路径+01背包),布布扣,bubuko.com

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

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/GO-NO-1/p/3745394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!