-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Knapsack.cpp
More file actions
55 lines (51 loc) · 1.1 KB
/
01Knapsack.cpp
File metadata and controls
55 lines (51 loc) · 1.1 KB
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
#include <iostream>
#include <climits>
#include <cstring>
using namespace std;
// recursive
int knapsack(int *wts,int *prices,int N,int W){
if(N==0 || W==0) return 0;
int inc = 0 ,exc = 0;
// /if include
// sirf tab jabb hamare thale ki capacity <= ho wt se
if(wts[N-1]<=W){
inc = prices[N-1] + knapsack(wts,prices,N-1,W-wts[N-1]);
}
// agar exclude
exc = 0 + knapsack(wts,prices,N-1,W);
return max(inc,exc);
}
int knapsackDP(int *wts,int *prices,int N,int W){
int dp[1000][1000];
for(int i=0;i<=N;i++){
for(int w=0;w<=W;w++){
if(i==0 || w==0){
dp[i][w] = 0;
}
else{
int inc=0;int exc = 0;
if(wts[i-1] <= w ){
inc = prices[i-1] + dp[i-1][w-wts[i-1]];
}
// excide price
exc = dp[i-1][w];
dp[i][w] = max(inc,exc);
}
}
}
for(int i=0;i<=N;i++){
for(int j=0;j<=W;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
return dp[N][W];
}
int main(){
int wts[] = {2,7,3,4};
int prices[] = {5,20,20,10};
int N,W;
cin>>N>>W;
cout<<knapsack(wts,prices,N,W)<<endl;
cout<<knapsackDP(wts,prices,N,W);
}