-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_5.py
29 lines (26 loc) · 1.18 KB
/
13_5.py
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
def fastMaxVal(toConsider, avail, memo={}):
"""toConsiderを品物のリスト、availを重さ
memoは再帰呼び出しによってのみ使われるとする
それらをパラメータとする0/1ナップザック問題の解である
総価値と品物のリストからなるタプルを返す"""
if (len(toConsider), avail) in memo:
result = memo[(len(toConsider), avail)]
elif toConsider == [] or avail == 0:
result = (0, ())
elif toConsider.getWeight() > avail:
#右側の分岐のみを探索する
result = fastMaxVal(toConsider[1:], avail, memo)
else:
nextItem = toConsider[0]
#左側の分岐を探索する
withVal, withToTake = fastMaxVal(toConsider[1:], avail-nextItem.getWeight(), memo)
withVal += nextItem.getValue()
#右側の分岐を探索する
withoutVal, withoutToTake = fastMaxVal(toConsider[1:], avail ,memo)
#より良い分岐を選択
if withVal > withoutVal:
result = (withVal, withToTake + (nextItem, ))
else:
result = (withoutVal, withoutToTake)
memo[(len(toConsider), avail)] = result
return result