Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <queue>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <cstring>
  21. #include <climits>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. using namespace std;
  25. #define REP(i,n) for(int i=0; i<n; i++)
  26. #define FOR(i,st,end) for(int i=st;i<end;i++)
  27. #define db(x) cout << (#x) << " = " << x << endl;
  28. #define mp make_pair
  29. #define pb push_back
  30. typedef long long int ll;
  31.  
  32. const int MAX = 1024;
  33. const int INF = 999999999;
  34.  
  35. int N, O, n, t, cs;
  36. int nitro[MAX], oxi[MAX], w[MAX];
  37. int dp[MAX][22][80], visited[MAX][22][80];
  38.  
  39. int solve(int i, int co, int cn)
  40. {
  41. if(dp[i][co][cn]!=-1){
  42. return dp[i][co][cn];
  43. }
  44. if(co==0&&cn==0){
  45. dp[i][co][cn]= 0;
  46. }
  47. else if(i==0){//if we considered all cylinders and if co and cn are still not equal to zero
  48. //then its not possible to satisfy the required condition. Hence return INF in order to eliminate all
  49. //recursive paths leading to this status
  50. dp[i][co][cn]= INF;
  51. }else{
  52. //either select the ith cylinder or move on to i-1th cylinder
  53. //Be careful about the index:
  54. //ith cylinder oxi[i-1] capacity of oxygen and nitro[i-1] capacity of nitrogen
  55. dp[i][co][cn]=min(solve(i-1,co,cn),solve(i-1,max(co-oxi[i-1],0),max(cn-nitro[i-1],0))+w[i-1]);
  56. }
  57. return dp[i][co][cn];
  58. }
  59.  
  60. int main()
  61. {
  62. scanf("%d", &t);
  63. while(t--){
  64.  
  65. scanf("%d %d %d", &O, &N, &n);
  66. for(int i=0;i<n+1;i++)
  67. for(int j=0;j<O+1;j++)
  68. for(int k=0;k<N+1;k++)
  69. dp[i][j][k]=-1;
  70.  
  71. for(int i = 0; i < n; i++)
  72. scanf("%d %d %d", &oxi[i], &nitro[i], &w[i]);
  73. //dp[n][O][N] gives the minimum total weight of cylinders required to have O and N capacity of oxygen
  74. //and nitrogen (capacity of oxygen and nitrogen can exceed)
  75. printf("%d\n", solve(n, O, N));
  76. }
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement