Advertisement
MinhNGUYEN2k4

Untitled

May 15th, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define Task "test"
  4. //#define READFILE freopen(Task".INP", "r", stdin)
  5. #define READFILE freopen("test.inp", "r", stdin)
  6. #define WRITEFILE freopen(Task".OUT", "w", stdout)
  7. #define oo 1e18
  8. #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  9. #define mp make_pair
  10. #define pb push_back
  11. #define X first
  12. #define Y second
  13. #define watch(x) cout << (#x) << " = " << x << endl
  14. #define debug(x) cout << (#x) << " = " << x << endl
  15. #define all(x) x.begin(), x.end()
  16. #define sz(x) x.size()
  17. #define endl '\n'
  18. #define max3(a,b,c) max(max(a, b), c)
  19. #define max4(a,b,c,d) max(max(a, b), max(c, d))
  20. #define min4(a,b,c,d) min(min(a, b), min(c, d))
  21. #define debug4(a,b,c,d) watch(a);watch(b);watch(c);watch(d)
  22. #define ever (;true;)
  23. #define maxn 505
  24.  
  25. using namespace std;
  26.  
  27. typedef pair < int, int > ii;
  28. typedef pair < int, ii > iii;
  29. typedef pair < ii, ii > iiii;
  30. typedef vector < int > vi;
  31. typedef vector < ii > vii;
  32. typedef vector < vi > vvi;
  33. typedef vector < iii > viii;
  34. typedef vector < vii > vvii;
  35. typedef vector < iiii > viiii;
  36. typedef vector < vvi > vvvi;
  37.  
  38. const int MOD = 1e9 + 7;
  39. const int dx[] = {-1, 1, 0, 0};
  40. const int dy[] = {0, 0, -1, 1};
  41.  
  42. const int N = 1e3 + 5;
  43. const int K = 10 + 5;
  44.  
  45. int n, m;
  46. int w[N], v[N];
  47.  
  48. int Vres = 0;
  49. int nRes = 0, res[N];
  50. //mảng chứa gói hàng kết quả
  51. int nCur = 0, cur[N];
  52. //mảng chứa gói hàng đang xét
  53.  
  54. void init(){
  55.     FAST;
  56.     #ifndef ONLINE_JUDGE
  57.     READFILE;
  58.     freopen("test.out", "w", stdout);
  59.     #endif // ONLINE_JUDGE
  60.     cin >> n >> m;
  61.     for (int i = 1; i <= n; ++i) cin >> w[i] >> v[i];
  62. }
  63.  
  64. void btr(int i, int curW, int curV){
  65.     //ĐÃ xét i-1 vật đầu tiên, sử dụng curW trọng lượng và có giá trị là curV
  66.     //nếu đã xét hết n vật thì cập nhật kết quả
  67.     if (i == n + 1){
  68.         if (Vres < curV){
  69.             Vres = curV;
  70.             nRes = nCur;
  71.             for (int i = 1; i <= nRes; ++i) res[i] = cur[i];
  72.         }
  73.         return;
  74.     }
  75.     //nếu bỏ qua vật thứ i
  76.     btr(i + 1, curW, curV);
  77.     //nếu mang theo vật thứ i
  78.     if (curW + w[i] <= m){
  79.         //cập nhật mảng gói hàng đang xét
  80.         cur[++nCur] = i;
  81.         btr(i + 1, curW + w[i], curV + v[i]);
  82.         //trả lại trạng thái ban đầu
  83.         --nCur;
  84.     }
  85. }
  86.  
  87. signed main(){
  88.     init();
  89.     btr(1, 0, 0);
  90.     cout << Vres << endl;
  91.     for (int i = 1; i <= nRes; ++i) cout << res[i] << ' ';
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement