-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_FractionalKnapsack.cpp
166 lines (135 loc) · 3.61 KB
/
GFG_FractionalKnapsack.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
https://practice.geeksforgeeks.org/problems/fractional-knapsack-1587115620/1
Fractional Knapsack
https://binarysearch.com/problems/Fractional-Knapsack
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
struct Item{
int value;
int weight;
};
// } Driver Code Ends
//class implemented
/*
struct Item{
int value;
int weight;
};
*/
class Solution
{
public:
static bool compareStruct(const Item &a, const Item &b)
{
double ppw1 = (double)a.value/(double)a.weight; // profit per weight
double ppw2 = (double)b.value/(double)b.weight; // profit per weight
// if(ppw1 > ppw2) return true;
// else false;
return ppw1 > ppw2;
}
double fractionalKnapsack2(int W, Item arr[], int n)
{
sort(arr, arr+n, compareStruct);
double maxSum=0;
for(int i=0; i<n; i++)
{
int val = arr[i].value;
int wgt = arr[i].weight;
if(wgt <= W)
{
maxSum+= val;
W-=wgt;
}
else
{
maxSum+= ((double)val/(double)wgt)*W;
break;
}
}
return maxSum;
}// end
//Function to get the maximum total value in the knapsack.
double fractionalKnapsack1(int W, Item arr[], int n)
{
auto compare = [](const pair<int,float> &a, const pair<int,float> &b)
{
return a.second > b.second;
};
// sort(arr, arr+n, compareStruct);
vector<pair<int,float>> itm(n);
double maxSum=0.0;
for(int i=0; i<n; i++)
{
itm[i]={i, arr[i].value/(arr[i].weight*1.0)};
}
sort(itm.begin(), itm.end(), compare);
for(auto x: itm)
{
int i = x.first;
int wgt = arr[i].weight;
if(wgt <= W)
{
maxSum+= arr[i].value;
W-=wgt;
}
else if(W>0)
{
maxSum+= (double)x.second*W;
break;
}
}
return maxSum;
}// end
struct cmpPQ
{
bool operator()(const Item &a, const Item &b)
{
double ppw1 = (double)a.value/(double)a.weight; // profit per weight
double ppw2 = (double)b.value/(double)b.weight; // profit per weight
// if(ppw1 > ppw2) return true;
// else false;
return ppw1 < ppw2;
}
};
double fractionalKnapsack(int W, Item arr[], int n)
{
double maxSum=0.0;
priority_queue<struct Item, vector<struct Item>, cmpPQ> pq(arr, arr+n);
while(!pq.empty() && W)
{
int itemWgt = pq.top().weight;
int itemVal = pq.top().value;
double ppw = (double)itemVal/(double)itemWgt;
// cout<<itemWgt<<" "<<itemVal<< " "<< W<< " "<<endl;
int cwgt = min(W, itemWgt);
maxSum += ppw*cwgt;
W = W - cwgt;
pq.pop();
}
return maxSum;
}// end
};
// { Driver Code Starts.
int main()
{
int t;
//taking testcases
cin>>t;
cout<<setprecision(2)<<fixed;
while(t--){
//size of array and weight
int n, W;
cin>>n>>W;
Item arr[n];
//value and weight of each item
for(int i=0;i<n;i++){
cin>>arr[i].value>>arr[i].weight;
}
//function call
Solution ob;
cout<<ob.fractionalKnapsack(W, arr, n)<<endl;
}
return 0;
} // } Driver Code Ends