forked from Anuragcoderealy/Codenewhacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01KnapSack.cpp
52 lines (51 loc) · 1.07 KB
/
01KnapSack.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
#include<iostream>
#include<vector>
using namespace std;
void knapsack(int wat[],int val[],int w,int n){
int t[n+1][w+1];
for(int i=0;i<=n;i++){
t[i][0]=0;
}
for(int i=0;i<=w;i++){
t[0][i]=0;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=w;j++){
if(wat[i-1]<=j){
t[i][j]=max(val[i-1]+t[i-1][j-wat[i-1]],t[i-1][j]);
}
else{
t[i][j]=t[i-1][j];
}
}
}
vector<int> v;
int i=n,j=w;
while(i!=0||j!=0){
if(t[i][j]!=t[i-1][j]){
v.push_back(i);
i--;
j=j-wat[i];
}
else{
i--;
}
}
cout<<"\nMax profit:"<<t[n][w];
cout<<endl;
for(i=0;i<v.size();i++){
cout<<"Item:"<<v[i]<<"-Weight:"<<wat[v[i]-1]<<" value:"<<val[v[i]-1]<<" "<<endl;
}
}
int main(){
int n=5;
int w=11;
int val[]={1,6,18,22,28};
int wat[]={1,2,5,6,7};
knapsack(wat,val,w,n);
return 0;
}
//Output:
//Max profit:40
//Item:4-Weight:6 value:22
//Item:3-Weight:5 value:18