[关闭]
@Chilling 2017-02-16T09:56:02.000000Z 字数 3516 阅读 839

HDU-3339: In Action(dijkstra+01背包)

最短路


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

题意:基地在0点,从基地到需要炸毁的点1~n有一定距离,每单位距离要耗费一单位的油。这1~n个点有自己的能量值,你至少需要减少他们能量总和的一半。问最少耗费的油是多少,如果不能完成输入impossible。

分析:求出从0到其他点的最短距离dis[1]~dis[n],然后又知道每个点的能量值pow[1]~pow[n],求得总和sum,sum/2+1就是至少炸毁的能量,简单的01背包。

  1. void dij()
  2. {
  3. dis[0]=0;
  4. priority_queue<node>q;
  5. q.push(node(0,dis[0]));
  6. while(!q.empty())
  7. {
  8. node now,next;
  9. now=q.top();
  10. q.pop();
  11. if(vis[now.en]==1)
  12. continue;
  13. vis[now.en]=1;
  14. for(int i=0;i<v[now.en].size();i++)
  15. {
  16. next=v[now.en][i];
  17. if(dis[next.en]>now.len+next.len)
  18. {
  19. dis[next.en]=now.len+next.len;
  20. q.push(node(next.en,dis[next.en]));
  21. }
  22. }
  23. }
  24. }
  1. void DP()
  2. {
  3. int i,j,x;
  4. for(i=1;i<=n;i++)
  5. sumd+=dis[i];
  6. for(i=1;i<=n;i++)
  7. for(j=sumd;j>=dis[i];j--)
  8. dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
  9. for(i=1;i<=sumd;i++)
  10. if(dp[i]>=sump) //若大于或者等于了总能量的一半
  11. {
  12. x=i;
  13. break;
  14. }
  15. printf("%d\n",x);
  16. }
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<queue>
  5. #define INF 1e9
  6. using namespace std;
  7. int dis[111],vis[111],n,m,p[111],dp[11111];
  8. int sump,sumd;
  9. struct node
  10. {
  11. int en,len;
  12. node(int en1=0,int len1=0)
  13. {
  14. en=en1;len=len1;
  15. }
  16. friend bool operator <(node x,node y)
  17. {
  18. return x.len>y.len;
  19. }
  20. };
  21. vector<node> v[11111];
  22. void dij()
  23. {
  24. dis[0]=0;
  25. priority_queue<node>q;
  26. q.push(node(0,dis[0]));
  27. while(!q.empty())
  28. {
  29. node now,next;
  30. now=q.top();
  31. q.pop();
  32. if(vis[now.en]==1)
  33. continue;
  34. vis[now.en]=1;
  35. for(int i=0;i<v[now.en].size();i++)
  36. {
  37. next=v[now.en][i];
  38. if(dis[next.en]>now.len+next.len)
  39. {
  40. dis[next.en]=now.len+next.len;
  41. q.push(node(next.en,dis[next.en]));
  42. }
  43. }
  44. }
  45. }
  46. void DP()
  47. {
  48. int i,j,x;
  49. for(i=1;i<=n;i++)
  50. sumd+=dis[i];
  51. for(i=1;i<=n;i++)
  52. for(j=sumd;j>=dis[i];j--)
  53. dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
  54. for(i=1;i<=sumd;i++)
  55. if(dp[i]>=sump)
  56. {
  57. x=i;
  58. break;
  59. }
  60. printf("%d\n",x);
  61. }
  62. int main()
  63. {
  64. int t,a,b,c,i;
  65. scanf("%d",&t);
  66. while(t--)
  67. {
  68. sump=0,sumd=0;
  69. for(i=0;i<111;i++)
  70. dis[i]=INF;
  71. memset(vis,0,sizeof(vis));
  72. memset(p,0,sizeof(p));
  73. memset(dp,0,sizeof(dp));
  74. scanf("%d%d",&n,&m);
  75. while(m--)
  76. {
  77. scanf("%d%d%d",&a,&b,&c);
  78. v[a].push_back(node(b,c));
  79. v[b].push_back(node(a,c));
  80. }
  81. for(i=1;i<=n;i++)
  82. {
  83. scanf("%d",&p[i]);
  84. sump+=p[i];
  85. }
  86. sump=sump/2+1; //一半以上
  87. dij();
  88. if(dis[1]==INF)
  89. printf("impossible\n");
  90. else
  91. DP();
  92. for(i=0;i<=n;i++)
  93. v[i].clear();
  94. }
  95. return 0;
  96. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注