-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathfractional_knapsack.py
More file actions
29 lines (23 loc) · 841 Bytes
/
fractional_knapsack.py
File metadata and controls
29 lines (23 loc) · 841 Bytes
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
# Uses python3
import sys
def get_optimal_value(capacity, weights, values):
opt_value = 0
items = list(zip(values, weights))
items.sort(key=lambda item: item[0] / item[1], reverse=True)
values = [item[0] for item in items]
weights = [item[1] for item in items]
for value, weight in zip(values, weights):
if capacity == 0:
return opt_value
min_weight = min(weight, capacity)
opt_value += min_weight * (value / weight)
weight -= min_weight
capacity -= min_weight
return opt_value
if __name__ == "__main__":
data = list(map(int, sys.stdin.read().split()))
n, capacity = data[0:2]
values = data[2:(2 * n + 2):2]
weights = data[3:(2 * n + 2):2]
opt_value = get_optimal_value(capacity, weights, values)
print("{:.4f}".format(opt_value))