forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractional_knapsack.cpp
64 lines (54 loc) · 1.27 KB
/
fractional_knapsack.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
#include <bits/stdc++.h>
using namespace std;
typedef struct {
double v;
double w;
double ratios;
}Items;
Items Item[10000];
bool comp(Items a,Items b){
return(a.ratios > b.ratios);
}
double get_optimal_value(long int capacity, vector<double> weights, vector<double> values) {
double value = 0.0;
long int n = weights.size();
long int i;
for(i = 0; i < n; i++){
Item[i].v = values[i];
Item[i].w = weights[i];
Item[i].ratios = (double)(values[i]/weights[i]);
}
sort(Item, Item + n, comp);
i = 0;
double curr = 0.0, weight = 0.0;
for(i = 0; i < n; i++){
if(weight + Item[i].w <= capacity)
{
value = value + Item[i].v;
weight = weight + Item[i].w;
}
else
{
curr = capacity - weight;
value = value + Item[i].v * ((double)curr / Item[i].w);
break;
}
}
// write your code here
return value;
}
int main() {
long int n;
long int capacity;
cin >> n >> capacity;
vector<double> values(n);
vector<double> weights(n);
for (int i = 0; i < n; i++) {
cin >> values[i] >> weights[i];
}
double optimal_value = get_optimal_value(capacity, weights, values);
//cout.precision(10);
//cout << optimal_value << endl;
printf("%.4f\n", optimal_value);
return 0;
}