1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <bits/stdc++.h> using namespace std;
int main() { int N = 4; int backpack = 10; vector<int> weight = {0, 2, 3, 4, 5}; vector<int> value = {0, 5, 6, 7, 8}; vector<vector<int>> dp(N + 1, vector<int>(backpack + 1, 0)); vector<vector<bool>> chosen(N + 1, vector<bool>(backpack + 1, false));
for (int i = 1; i <= N; i++) { for (int j = 0; j <= backpack; j++) { if (weight[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); if (dp[i][j] > dp[i - 1][j]) { chosen[i][j] = true; } } else { dp[i][j] = dp[i - 1][j]; } } }
cout << "max value: " << dp[N][backpack] << endl;
int i = N, j = backpack; vector<int> selectedItems; while (i > 0 && j > 0) { if (chosen[i][j]) { selectedItems.push_back(i); j -= weight[i]; } i--; }
cout << "selected: "; for (int i = selectedItems.size() - 1; i >= 0; i--) { cout << selectedItems[i] << " "; } cout << endl;
return 0; }
|