首页 > 代码库 > hdu 3339 In Action (最短路径+01背包)
hdu 3339 In Action (最短路径+01背包)
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
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.
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).
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
Recommend
lcy | We have carefully selected several similar problems for you: 3338 1546 3333 3341 3335
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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。