hdu 2955 01背包变化

  1. /*********************************************
  2. 我们来模拟这个过程,首先:
  3. 将全部银行的钱当做背包的最大体积
  4. 那么每家银行的钱则当做物品的体积
  5. 那么被抓的概率当做物品的价值
  6. 那么便容易推出我们所需要的动态方程:
  7. dp[i]=max(dp[i],(dp[i-money]*(1-rp)));  // money 当前银行钱,rp 当前被抓概率
  8. dp求出不被抓的概率
  9. 一定得反过来看,因为一开始是被抓的概率的话,F[0]=0,就无法计算乘法的概率了。
  10. **********************************************************************************/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <iostream>
  14. #include <algorithm>
  15. using namespace std;
  16. double f[10050];
  17. double val[10050];
  18. int weight[10050];
  19. int main()
  20. {
  21. freopen("read.txt", "r", stdin);
  22. int T;
  23. scanf("%d", &T);
  24. while(T--)
  25. {
  26. double p;
  27. int n;
  28. int sum = 0;
  29. scanf("%lf%d", &p, &n);
  30. memset(f, 0, sizeof(f) );
  31. for(int i=0; i<n; i++)
  32. {
  33. scanf("%d%lf", &weight[i], &val[i]);
  34. sum += weight[i];
  35. }
  36. f[0] = 1;
  37. for(int i=0; i<n; i++)
  38. {
  39. for(int j=sum; j>=weight[i]; j--)
  40. {
  41. f[j]=max(f[j], (f[j-weight[i]])*(1-val[i]));
  42. }
  43. }
  44. for(int i=sum; i>=0; i--)
  45. {
  46. if(f[i] >= p)
  47. {printf("%d ", i);break;}
  48. }
  49. }
  50. return 0;
  51. }
  52. /*
  53. Problem Description
  54. The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.
  55. For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.
  56. His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
  57. Input
  58. The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj .
  59. Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
  60. Output
  61. For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.
  62. Notes and Constraints
  63. 0 < T <= 100
  64. 0.0 <= P <= 1.0
  65. 0 < N <= 100
  66. 0 < Mj <= 100
  67. 0.0 <= Pj <= 1.0
  68. A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
  69. Sample Input
  70. 3
  71. 0.04 3
  72. 1 0.02
  73. 2 0.03
  74. 3 0.05
  75. 0.06 3
  76. 2 0.03
  77. 2 0.03
  78. 3 0.05
  79. 0.10 3
  80. 1 0.03
  81. 2 0.02
  82. 3 0.05
  83. Sample Output
  84. 2
  85. 4
  86. 6
  87. */





附件列表

    原文地址:https://www.cnblogs.com/sober-reflection/p/d432689528ed551ad5a34fa6b7033455.html