-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-1Knapsack.cpp
More file actions
84 lines (69 loc) · 1.7 KB
/
Copy path0-1Knapsack.cpp
File metadata and controls
84 lines (69 loc) · 1.7 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
int max(int a, int b){
if(a<b)
return b;
else
return a;
}
struct item {
int value, weight;
};
bool compare(item a, item b) {
return a.weight < b.weight;
}
int main() {
int n, w;
cout << "Enter the number of items: ";
cin >> n;
item items[n];
cout << "Enter the value and weight of each item: " << endl;
for (int i = 0; i < n; i++) {
cin >> items[i].value >> items[i].weight;
}
sort(items, items+n, compare);
cout << "Enter the capacity of the knapsack: ";
cin >> w;
int V[n+1][w+1];
for (int i = 0; i <= n; i++)
{
V[i][0] = 0;
}
for (int i = 0; i <= w; i++)
{
V[0][i] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= w; j++)
{
if (j-items[i-1].weight<0)
{
V[i][j] = V[i-1][j];
}
else{
V[i][j] = max(V[i-1][j], items[i-1].value + V[i-1][j-items[i-1].weight]);
}
}
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= w; j++)
{
cout<<V[i][j]<<"\t";
}
cout<<"\n";
}
cout <<"The maximum value of items that can be put into the knapsack is: " << V[n][w] << endl;
int res = V[n][w];
for (int i = n; i > 0 && res > 0; i--) {
if (res == V[i - 1][w])
continue;
else {
cout<<items[i-1].weight<<" ";
res = res - items[i-1].value;
w = w - items[i-1].weight;
}
}
return 0;
}