'''
Created on 2012-11-4
@author: Pandara
'''
#input
import sys
m = input("backpack size:")
n = input("objects sum:")
objects_s = []#sequence s
objects_v = []#sequence v
input_str = raw_input("input objects' s:(a b c...)")
input_str = input_str.split(" ")
objects_s = [int(str) for str in input_str]
input_str = raw_input("input objects' v:(a b c...)")
input_str = input_str.split(" ")
objects_v = [int(str) for str in input_str]
v = [([0] * (m + 1)) for i in xrange(n + 1)]
#core
for i in range(n + 1)[1:]:
for j in range(m + 1)[1:]:
v[i][j] = v[i - 1][j]
if objects_s[i - 1] <= j:
v[i][j] = max((v[i][j], v[i - 1][j - objects_s[i - 1]] + objects_v[i - 1]))
for i in range(n + 1):
for j in range(m + 1):
sys.stdout.write("\t%d" % v[i][j])
sys.stdout.write("\n")
0/1背包问题算法的python实现
最新推荐文章于 2024-08-18 23:14:37 发布