-
Notifications
You must be signed in to change notification settings - Fork 47
/
0_1_knapsack_iterative.cpp
46 lines (35 loc) · 1.02 KB
/
0_1_knapsack_iterative.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
#include<bits/stdc++.h>
#define MAX 1005
using namespace std;
vector < vector<int> > dp(MAX, vector<int> (MAX, -1));
int knapsack(vector <int> weight, vector <int> value, int maxweight, int index)
{
//base condition
if( (index < 0) || (maxweight <= 0) )
return 0;
if(dp[index][maxweight] == -1)
{
if(weight[index] <= maxweight)
dp[index][maxweight] = max(value[index] + knapsack(weight, value, maxweight - weight[index], index-1), knapsack(weight, value, maxweight, index-1));
else
dp[index][maxweight] = knapsack(weight, value, maxweight, index-1);
}
return dp[index][maxweight];
}
int main()
{
int n;
cout<<"Enter the number of items: ";
cin>>n;
vector <int> weight(n), value(n);
cout<<"enter weights: "<<endl;
for(int i=0; i<n; i++)
cin>>weight[i];
cout<<"enter values: "<<endl;
for(int i=0; i<n; i++)
cin>>value[i];
int maxweight;
cout<<"Enter maximum capacity of knapsack: ";
cin>>maxweight;
cout<<knapsack(weight, value, maxweight, n-1);
}