-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.cpp
More file actions
55 lines (50 loc) · 1.11 KB
/
Copy pathknapsack.cpp
File metadata and controls
55 lines (50 loc) · 1.11 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
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<functional>
#include<unordered_map>
using namespace std;
int Knapsack(int wt[], int val[], int W, int n) {
int t[n + 1][W + 1]; // DP matrix
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (i == 0 || j == 0) // base case
t[i][j] = 0;
else if (wt[i - 1] <= j) { // current wt can fit in bag
int val1 = val[i - 1] + t[i - 1][j - wt[i - 1]]; // take current wt
int val2 = t[i - 1][j]; // skip current wt
t[i][j] = max(val1, val2);
}
else if (wt[i - 1] > j) // current wt doesn't fit in bag
t[i][j] = t[i - 1][j];
}
}
for (int i = 0; i < n+1; i++)
{
for (int j = 0; j < W+1; j++)
{
std::cout << t[i][j] << " ";
}
std::cout <<std::endl;
}
return t[n][W];
}
signed main() {
int n = 5;
int wt[] = {1,2,5,6,7};
int val[] = {1,6,18,22,28};
int W = 10;
cout << Knapsack(wt, val, W, n) << endl;
return 0;
}