-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedyKnapsack.cpp
More file actions
61 lines (51 loc) · 1.95 KB
/
greedyKnapsack.cpp
File metadata and controls
61 lines (51 loc) · 1.95 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter the number of items: ";
cin >> n;
vector<pair<double, pair<int, int>>> vp(n); // {value-to-weight ratio, {price, weight}}
cout << "Enter price and weight for each item: " << endl;
for (auto &i : vp) {
cin >> i.second.first >> i.second.second;
i.first = i.second.first * 1.0 / i.second.second; // Value-to-weight ratio
}
// Sort items by value-to-weight ratio in descending order
sort(vp.begin(), vp.end(), [](auto &a, auto &b) {
return a.first > b.first;
});
// Print the sorted items
cout << "Sorted items (ratio price weight):" << endl;
for (auto &i : vp) {
cout << i.first << ' ' << i.second.first << ' ' << i.second.second << endl;
}
int m;
cout << "Enter the maximum weight capacity: ";
cin >> m;
int totalWeight = 0;
double totalValue = 0.0;
vector<pair<double, pair<int, int>>> solutionVector; // Fractional solution vector
for (int i = 0; i < n; ++i) {
if (totalWeight + vp[i].second.second <= m) {
// Take the entire item
totalWeight += vp[i].second.second;
totalValue += vp[i].second.first;
solutionVector.push_back({1.0, {vp[i].second.first, vp[i].second.second}});
} else {
// Take a fraction of the item
double fraction = (m - totalWeight) * 1.0 / vp[i].second.second;
totalWeight += fraction * vp[i].second.second;
totalValue += fraction * vp[i].second.first;
solutionVector.push_back({fraction, {vp[i].second.first, vp[i].second.second}});
break;
}
}
// Print the solution
cout << "Solution (fraction price weight):" << endl;
for (auto &i : solutionVector) {
cout << i.first << ' ' << i.second.first << ' ' << i.second.second << endl;
}
cout << "Total value: " << totalValue << endl;
return 0;
}