mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-07 05:04:20 +00:00
Add the chapter of greedy. (#633)
Add the section of fractional knapsack.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
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}")
|
||||
Reference in New Issue
Block a user