mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 10:36:28 +08:00
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
|
"""
|
||
|
File: fractional_knapsack.py
|
||
|
Created Time: 2023-07-19
|
||
|
Author: Krahets (krahets@163.com)
|
||
|
"""
|
||
|
|
||
|
|
||
|
class Item:
|
||
|
"""物品"""
|
||
|
|
||
|
def __init__(self, w: int, v: int):
|
||
|
self.w = w # 物品重量
|
||
|
self.v = v # 物品价值
|
||
|
|
||
|
|
||
|
def fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:
|
||
|
"""分数背包:贪心"""
|
||
|
# 创建物品列表,包含两个属性:重量、价值
|
||
|
items = [Item(w, v) for w, v in zip(wgt, val)]
|
||
|
# 按照单位价值 item.v / item.w 从高到低进行排序
|
||
|
items.sort(key=lambda item: item.v / item.w, reverse=True)
|
||
|
# 循环贪心选择
|
||
|
res = 0
|
||
|
for item in items:
|
||
|
if item.w <= cap:
|
||
|
# 若剩余容量充足,则将当前物品整个装进背包
|
||
|
res += item.v
|
||
|
cap -= item.w
|
||
|
else:
|
||
|
# 若剩余容量不足,则将当前物品的一部分装进背包
|
||
|
res += (item.v / item.w) * cap
|
||
|
# 已无剩余容量,因此跳出循环
|
||
|
break
|
||
|
return res
|
||
|
|
||
|
|
||
|
"""Driver Code"""
|
||
|
if __name__ == "__main__":
|
||
|
wgt = [10, 20, 30, 40, 50]
|
||
|
val = [50, 120, 150, 210, 240]
|
||
|
cap = 50
|
||
|
n = len(wgt)
|
||
|
|
||
|
# 贪心算法
|
||
|
res = fractional_knapsack(wgt, val, cap)
|
||
|
print(f"不超过背包容量的最大物品价值为 {res}")
|