shaikat0088

Uva10130

Mar 29th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. /* Combination using DP
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define SZ 500
  5. int N,R;
  6. int Dp_arr[SZ][SZ];
  7.  
  8. void init()
  9. {
  10. for (int i = 0; i < SZ; i++){
  11. for (int j = 0; j < SZ; j++){
  12. Dp_arr[i][j] = -1;
  13. }
  14. }
  15. }
  16.  
  17. void take_input()
  18. {
  19. scanf_s("%d", &N);
  20. scanf_s("%d", &R);
  21. }
  22.  
  23. int Combination(int n, int r){
  24. if (r == 1){
  25. return n;
  26. }
  27. if (n == r){
  28. return 1;
  29. }
  30.  
  31. if (Dp_arr[n][r] != -1){
  32. return Dp_arr[n][r];
  33. }
  34.  
  35. Dp_arr[n][r] = Combination(n - 1, r) + Combination(n - 1, r - 1);
  36.  
  37. return Dp_arr[n][r];
  38. }
  39.  
  40. int main()
  41. {
  42.  
  43. init();
  44. take_input();
  45. int res = Combination(N, R);
  46.  
  47. printf("%d\n", res);
  48.  
  49. return 0;
  50. }
  51. */
  52.  
  53.  
  54.  
  55.  
  56. /*void take_input(){
  57. scanf("%d", &N);
  58.  
  59. for (int i = 0; i < N; i++){
  60. scanf("%d %d", &Weight_arr[i], &Cost_arr[i]);
  61. }
  62. scanf("%d", &W);
  63. }*/
  64.  
  65.  
  66.  
  67.  
  68. //uva10130
  69.  
  70. #include <stdio.h>
  71. #include <math.h>
  72.  
  73.  
  74. #define SZ 500+510
  75. #define MX(a,b) a>b?a:b
  76.  
  77. int N, W,Test_cases,cap;
  78. int Weight_arr[SZ], Cost_arr[SZ];
  79. int Dp[SZ][SZ];
  80.  
  81.  
  82. void init()
  83. {
  84. for (int i = 0; i < SZ; i++){
  85. for (int j = 0; j < 33; j++){
  86. Dp[i][j] = -1;
  87. }
  88. }
  89. }
  90.  
  91. int napesake(int i, int w, int cap){
  92.  
  93. int profit1,profit2;
  94.  
  95. if (Dp[i][w] != -1){
  96. return Dp[i][w];
  97. }
  98.  
  99. if (i >= N){
  100. return 0;
  101. }
  102.  
  103. if (w + Weight_arr[i] <= cap){
  104. profit1 = Cost_arr[i] + napesake(i + 1, w + Weight_arr[i],cap);
  105. }
  106. else{
  107. profit1 = 0;
  108. }
  109.  
  110. profit2 = napesake(i + 1, w,cap);
  111.  
  112. return Dp[i][w] = MX(profit1, profit2);
  113. }
  114.  
  115. int main()
  116. {
  117.  
  118.  
  119. /*freopen("in.txt", "r", stdin);
  120. freopen("out.txt", "w", stdout);*/
  121.  
  122. scanf("%d", &Test_cases);
  123.  
  124. for (int i = 0; i < Test_cases; i++){
  125. int res = 0;
  126.  
  127.  
  128. scanf("%d", &N);
  129.  
  130. for (int i = 0; i < N; i++){
  131. scanf("%d %d", &Cost_arr[i],&Weight_arr[i]);
  132. }
  133. scanf("%d", &W);
  134.  
  135. for (int j = 0; j < W; j++){
  136.  
  137. scanf("%d", &cap);
  138. init();
  139. res += napesake(0, 0,cap);
  140. }
  141.  
  142. printf("%d\n", res);
  143. }
  144.  
  145.  
  146.  
  147. return 0;
  148.  
  149. }
Add Comment
Please, Sign In to add comment