-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathknapsack.cpp
61 lines (57 loc) · 1.17 KB
/
knapsack.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
// Knapsack Algorithm
#include <iostream>
#include<vector>
#include<climits>
using namespace std;
int max1(int a, int b) { return (a > b)? a : b; }
struct data{
int wt;
int val;
};
int** knapSack(int W, vector < data > d)
{
int** mat = 0;
mat = new int*[d.size()+1];
for(int i=0;i<=d.size();i++)
{
mat[i] = new int[W+1];
for(int w=0;w<=W;w++)
{
if (i == 0 || w == 0)
mat[i][w]=0;
else if (d[i-1].wt <= W)
mat[i][w] = max1( d[i-1].val + mat[i-1][w-d[i-1].wt],mat[i-1][w]);
else
mat[i][w] = mat[i-1][w];
}
}
return mat;
}
int main()
{
int n;
cin>>n;
int W;
cin>>W;
vector < data > d(n);
for(int i=0;i<n;i++)
{
cin >> d[i].val >> d[i].wt;
}
int** mat = knapSack(W , d );
int i=d.size();
int w=W;
/*while (mat[i][w]!=0){
if(mat[i][w]==mat[i-1][w] || w<d[i-1].wt){
i--;
}
else{
cout<<d[i-1].val<<" extracted ";
w=w-d[i-1].wt;
i--;
cout<<"rem wt. "<<w<<endl;
}
}*/
cout<<"\nMax value :"<<mat[d.size()][W] << "\n";
return 0;
}