-
Notifications
You must be signed in to change notification settings - Fork 1
/
knapsack_DP.cpp
95 lines (95 loc) · 2.19 KB
/
knapsack_DP.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Knapsack Problem with dynamic programming
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i,a,n) for(int i=a;i<n;i++)
#define fre(i,a,b) for(int i=a;i<=b;i++)
#define endl '\n'
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define mem(name, value) memset(name, value, sizeof(name))
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define case cout << "Case " << t++ << ": ";
// recursive case in knapsack Complexity O(2^n)
int knapsack_rec(vector<int> &wt, vector<int> &vl, int w, int n) {
if (n == 0 || w == 0) {
return 0;
}
if (wt[n - 1] <= w) {
return max(vl[n - 1] + knapsack_rec(wt, vl, w - wt[n - 1], n - 1), knapsack_rec(wt, vl, w, n - 1));
}
else {
return knapsack_rec(wt, vl, w, n - 1);
}
}
//optimised O(n*w)
int knapsack_TD(vector<int> &wt, vector<int> &vl, int w, int n) {
dp[n + 1][wt + 1];
mem(dp, -1);
if (n == 0 || w == 0) {
return 0;
}
if (dp[n][w] != -1)
{
return dp[n][w];
}
if (wt[n - 1] <= w) {
return dp[n][w] = max(vl[n - 1] + knapsack_TD(wt, vl, w - wt[n - 1], n - 1), knapsack_TD(wt, vl, w, n - 1));
}
else {
return dp[n][w] = knapsack_TD(wt, vl, w, n - 1);
}
return dp[n][w] = knapsack_TD(wt, vl, w, n - 1);
}
// T Complexity O(n*w)
int knapsack_BU(vector<int> &wt, vector<int> &vl, int w, int v) {
dp[n + 1][w + 1];
for (i = 0; i < n + 1; i++) {
for (j = 0; j < w + 1; j++) {
if (i == 0 || j == 0) {
dp[i][j] == 0;
}
else if (wt[i - 1] <= j) {
dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
}
else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[n][w];
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
int n, wt;
cin >> n >> wt;
vector<int> wt;
vector<int> vl;
int ipt;
fr(i, 0, n) {
cin >> ipt;
wt.pb(ipt);
}
fr(i, 0, n) {
cin >> ipt;
wt.pb(ipt);
}
cout << knapsack(wt, vl, w, n) << endl;
}
return 0;
}