用最naive的方法,将多个同一物品转化成不同物品,再用01背包求解
代码
import sys
def bag(n, t, w, v, s):
ww = [0 for i in range(n * n + 1)]
vv = [0 for i in range(n * n + 1)]
k = 1
for i in range(1, n + 1):
for j in range(1, s[i] + 1):
ww[k] = w[i]
vv[k] = v[i]
k += 1
ans = [0 for i in range(t + 1)]
for i in range(1, k):
for j in range(t, 0, -1):
if j >= ww[i]:
ans[j] = max(ans[j], ans[j - ww[i]] + vv[i])
return ans
if name == “main”:
n, t = map(int, input().strip().split())
w = [0 for i in range(n + 1)]
v = [0 for i in range(n + 1)]
s = [0 for i in range(n + 1)]
for i in range(1, n + 1):
w[i], v[i], s[i] = map(int, input().strip().split())
ans = bag(n, t, w, v, s)
print(ans[t])
666