-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractionalknapsack.cpp
More file actions
52 lines (48 loc) · 1 KB
/
Copy pathfractionalknapsack.cpp
File metadata and controls
52 lines (48 loc) · 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
#include <bits/stdc++.h>
using namespace std;
struct Item{
int value;
int weight;
};
// } Driver Code Ends
bool compare(struct Item a,struct Item b){
double r1=(double)a.value/a.weight;
double r2=(double)b.value/b.weight;
return r1>r2;
}
double fractionalKnapsack(int w, struct Item arr[],int n){
sort(arr,arr+n,compare);
int currentweight=0;
double finalvalue=0.0;
for(int i=0;i<n;i++)
{
if(currentweight+arr[i].weight<=w){
currentweight+=arr[i].weight;
finalvalue+=arr[i].value;
}
else
{
int remain=w-currentweight;
finalvalue+=arr[i].value*((double)remain/arr[i].weight);
break;
}
}
return finalvalue;
}
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
cout<<setprecision(2)<<fixed;
while(t--){
int n, W;
cin>>n>>W;
Item arr[n];
for(int i=0;i<n;i++){
cin>>arr[i].value>>arr[i].weight;
}
cout<<fractionalKnapsack(W, arr, n)<<endl;
}
return 0;
} // } Driver Code Ends