Algorithm FractionalKnapsack(S, W ):
Input: Set S of items, such that each item i ∈ S has a positive benefit bi and a
positive weight wi; positive maximum total weight W
Output: Amount xi of each item i ∈ S that maximizes the total benefit while
not exceeding the maximum total weight W
for each item i ∈ S do
xi ← 0
vi ← bi/wi // value index of item i
w ← 0 // total weight
while w < W and S ̸= ∅ do
remove from S an item i with highest value index
a ← min{wi, W − w} // more than W − w causes a weight overflow
xi ← a
w←w+a
我正在尝试在Ruby中实现上面的伪代码,我已成功实现了优先级队列,但我只需要有人为我解释这一行:
a←min {wi,W - w} //大于W - w导致重量溢出
min函数究竟应该做什么?以及如何实施?
答案 0 :(得分:1)
线a←min {wi,W-w}实际上给出'a'的值,该值是w [i]和W-w的最小值。
$product->test
您可以执行
if w[i] < W-w then a ← w[i]
otherwise a ← W-w