zoukankan      html  css  js  c++  java
  • hdu 3339 最短路+01背包

    1. #include <stdio.h>
    2. #include <queue>
    3. #include <iostream>
    4. #include <algorithm>
    5. using namespace std;
    6. #define INF 0xfffff
    7. #define MAX 200
    8. int path[MAX][MAX];
    9. int dis[MAX];
    10. bool sign[MAX];
    11. int val[MAX];
    12. int f[MAX * MAX];
    13. int sum;
    14. void initiaze(int n)
    15. {
    16. sum = 0;
    17. for(int i=0; i<=n; i++)
    18. {
    19. sign[i] = false; dis[i] = INF;
    20. for(int j=0; j<=n; j++)
    21. {
    22. path[i][j] = INF;
    23. }
    24. }
    25. }
    26. void input(int n, int m)
    27. {
    28. for(int i=0; i<n; i++)
    29. {
    30. int a, b, c;
    31. scanf("%d%d%d", &a, &b, &c);
    32. {
    33. if(path[a][b] > c)
    34. {
    35. path[a][b] = c;
    36. path[b][a] = c;
    37. }
    38. }
    39. }
    40. for(int i=1; i<=m; i++)
    41. {
    42. scanf("%d", &val[i]);
    43. sum += val[i];
    44. }
    45. }
    46. void Spfa(int n, int start)
    47. {
    48. queue<int>q;
    49. dis[start] = 0;
    50. sign[start] = true;
    51. q.push(start);
    52. while(!q.empty() )
    53. {
    54. int tp = q.front();
    55. q.pop();
    56. for(int i=0; i<=n; i++)
    57. {
    58. if(dis[i] > dis[tp] + path[tp][i])
    59. {
    60. dis[i] = dis[tp] + path[tp][i];
    61. if(!sign[i])
    62. {
    63. q.push(i);
    64. sign[i] = true;
    65. }
    66. }
    67. }
    68. sign[tp] = false;
    69. }
    70. }
    71. int main()
    72. {
    73. //freopen("read.txt", "r", stdin);
    74. int T;
    75. scanf("%d", &T);
    76. while(T--)
    77. {
    78. int n, m;
    79. scanf("%d%d", &n, &m);
    80. initiaze(n);
    81. input(m, n);
    82. Spfa(n, 0);
    83. memset(f, 0, sizeof(f) );
    84. sum = (sum/2 +1);
    85. int V = 0;
    86. bool flag = 0;
    87. for(int i=0; i<=m; i++) //以最短路径之和最为背包容量, 各个最短路径为放入背包的物体
    88. {
    89. V += dis[i];
    90. if(dis[i] >= INF)
    91. {flag =1; break;}
    92. }
    93. if(flag) {printf("impossible "); continue;}
    94. for(int i=1; i<=n; i++)
    95. {
    96. for(int j=V; j>=dis[i]; j--)
    97. {
    98. f[j] = max(f[j], f[j-dis[i]]+val[i] ); //每一站的输电量最为放入背包的物品的价值
    99. }
    100. }
    101. for(int i=1; i<=V; i++)
    102. {
    103. if(f[i] >= sum)
    104. {
    105. printf("%d ", i);
    106. break;
    107. }
    108. }
    109. }
    110. return 0;
    111. }
    112. /*
    113. Problem Description
    114. 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.
    115. 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.
    116. 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 vals, which forms a huge and complex electric network. Every electric val 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 val, 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.
    117. Now our commander wants to know the minimal oil cost in this action.
    118. Input
    119. The first line of the input contains a single integer T, specifying the number of testcase in the file.
    120. For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the vals(the IDs are 1,2,3...n), and the number of the roads between the val(bi-direction).
    121. 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.
    122. Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric val's power by ID order.
    123. Output
    124. The minimal oil cost in this action.
    125. If not exist print "impossible"(without quotes).
    126. Sample Input
    127. 2
    128. 2 3
    129. 0 2 9
    130. 2 1 3
    131. 1 0 2
    132. 1
    133. 3
    134. 2 1
    135. 2 1 3
    136. 1
    137. 3
    138. Sample Output
    139. 5
    140. impossible
    141. */





    附件列表

    • 相关阅读:
      R学习 第二篇:矩阵和数组
      R学习 第一篇:变量和向量
      PowerBI开发 第七篇:数据集和数据刷新
      PowerBI开发 第八篇:查询参数
      PowerBI开发 第六章:数据网管
      专用管理连接(DAC)和单用户模式
      PowerBI开发 第五篇:关系和交互
      PowerBI开发 第四篇:DAX 表达式基础
      PowerBI开发 第三篇:报表设计技巧
      分区表介绍
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/9556ce57776f645cfdc5ef9b6b106233.html
    Copyright © 2011-2022 走看看