-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.cpp
More file actions
47 lines (41 loc) · 887 Bytes
/
Copy pathimplementation.cpp
File metadata and controls
47 lines (41 loc) · 887 Bytes
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
// n items, capacity W
vector<int> dp(W+1, 0);
//0-1 Knapsack
for(int i=0; i<n; i++){
int w=weight[i], v=value[i];
for(int j=W; j>=w; j--){
dp[j] = max(dp[j], dp[j-w]+v);
}
}
//Complete Knapsack
for(int i=0; i<n; i++){
int w=weight[i], v=value[i];
for(int j=w; j<=W; j++){
dp[j] = max(dp[j], dp[j-w]+v);
}
}
//Multiple Knapsack
for(int i=0; i<n;i++){
int w=weight[i], v=value[i], k=count[i];
for (int p=1; k>0; p<<=1){
int take = min(p, k);
int ww = take*w;
int vv = take*v;
for (int j=W; j>=ww; j--){
dp[j] = max(dp[j], dp[j-ww]+vv);
}
k -= take;
}
}
//Mixed Knapsack
for(each item){
if(type == 0){
// 0-1 knapsack update
}
else if(type == 1){
// complete knapsack update
}
else{
// multiple knapsack (binary split)
}
}