-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-1knapsack.cpp
More file actions
64 lines (53 loc) · 1.81 KB
/
Copy path0-1knapsack.cpp
File metadata and controls
64 lines (53 loc) · 1.81 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int knapsack(int calorieLimit, vector<int>& calories, vector<int>& nutritionalValue, int n, vector<int>& includedItems) {
vector<vector<int>> dp(n + 1, vector<int>(calorieLimit + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= calorieLimit; w++) {
if (calories[i - 1] <= w) {
if (dp[i - 1][w] < dp[i - 1][w - calories[i - 1]] + nutritionalValue[i - 1]) {
dp[i][w] = dp[i - 1][w - calories[i - 1]] + nutritionalValue[i - 1];
} else {
dp[i][w] = dp[i - 1][w];
}
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
// Backtrack to find the included items
int w = calorieLimit;
for (int i = n; i > 0 && w > 0; i--) {
if (dp[i][w] != dp[i - 1][w]) {
includedItems.push_back(i - 1);
w -= calories[i - 1];
}
}
// Print the cost matrix
cout << "Cost Matrix (DP Table):\n";
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= calorieLimit; w+=100) {
cout << dp[i][w] << "\t";
}
cout << endl;
}
return dp[n][calorieLimit];
}
int main() {
vector<int> calories = {600, 400, 500, 300, 700};
vector<int> nutritionalValue = {80, 60, 70, 40, 90};
int calorieLimit = 2000;
int n = calories.size();
vector<int> includedItems;
int maxNutritionalValue = knapsack(calorieLimit, calories, nutritionalValue, n, includedItems);
cout << "Maximum Nutritional Value: " << maxNutritionalValue << endl;
// Print included items
cout << " list ";
for (int i : includedItems) {
cout << i << " ";
}
cout << endl;
return 0;
}