-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path0-1Knapsack.cpp
57 lines (54 loc) · 1.4 KB
/
0-1Knapsack.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
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
int knapsack(int profit[],int weight[],int n,int knapsack_size)
{
int solution[n+1][knapsack_size+1];
for(int i=0;i<n+1;i++)
{
for(int w=0;w<knapsack_size+1;w++)
{
if(i==0||w==0)
{
solution[i][w]=0;
}
else if(w>=weight[i-1])
{
solution[i][w]=max(solution[i-1][w],solution[i-1][w-weight[i-1]]+profit[i-1]);
}
else
{
solution[i][w]=solution[i-1][w];
}
}
}
for (int i = 0; i < n + 1; i++)
{
for (int w = 0; w < knapsack_size + 1; w++)
{
cout << solution[i][w] << "\t";
}
cout << endl;
}
return solution[n][knapsack_size];
}
int main()
{
int n,knapsack_size;
cout<<"Enter Number of Items:"<<endl;
cin>>n;
cout<<"Enter Knapsack Size:"<<endl;
cin>>knapsack_size;
int profit[n],weight[n];
for(int i=0;i<n;i++)
{
cout<<"Enter Details Of Item "<<i+1<<":"<<endl;
cout<<"Enter Profit:"<<endl;
cin>>profit[i];
cout<<"Enter Weight:"<<endl;
cin>>weight[i];
}
int total_profit=knapsack(profit,weight,n,knapsack_size);
cout<<"Maximum profit:"<<total_profit;
return 0;
}