python实现01背包 不同解法

import time
w=[1,3,5,7]
v=[2,4,6,8]
n=4
allw=10
begin=time.time()
s=sorted(list(zip(w,v)),reverse=True)
bestv=0
res=[0]*4
for i in range(4):
if s[i][0]<allw:
bestv+=s[i][1]
res[i]=1
allw-=s[i][0]
elif s[i][0]>=allw:
res[i]=0

print(bestv)
print(res)
end=time.time()
print(end-begin)


10
[1, 0, 0, 1]
0.03200888633728027



import time
w=[0,1,3,5,7]
v=[0,2,4,6,8]

allw=10
s=[0]*5
begin=time.time()
dp=[[0 for i in range(11)]for i in range(5)]

for i in range(5):
for j in range(11):
if j<w[i]:
dp[i][j]=dp[i-1][j]
else:
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i])

for i in range(5):
res=[]
for j in range(11):
res.append(dp[i][j])
print(res)

def back(i,j):
if i>0:
if dp[i][j]==dp[i-1][j]:
s[i]=0
back(i-1,j)
elif j-w[i]>=0 and dp[i][j]==dp[i-1][j-w[i]]+v[i]:
s[i]=1
back(i-1,j-w[i])

back(4,10)
print('------------------------------')
print(s)
end=time.time()
print(end-begin)


[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[0, 2, 2, 4, 6, 6, 6, 6, 6, 6, 6]
[0, 2, 2, 4, 6, 6, 8, 8, 10, 12, 12]
[0, 2, 2, 4, 6, 6, 8, 8, 10, 12, 12]
------------------------------
[0, 1, 1, 1, 0]
0.08184647560119629



import time
bestV=0
curW=0
curV=0
bestx=None
begin=time.time()
def backtrack(i):
global bestV,curW,curV,x,bestx
if i>=n:
if bestV<curV:
bestV=curV
bestx=x[:]
else:
if curW+w[i]<=c:
x[i]=True
curW+=w[i]
curV+=v[i]
backtrack(i+1)
curW-=w[i]
curV-=v[i]
x[i]=False
backtrack(i+1)

if __name__=='__main__':
n=4
c=10
w=[1,3,5,7]
v=[2,4,6,8]
x=[False for i in range(n)]
backtrack(0)
print(bestV)
print(bestx)
end=time.time()
print(end-begin)



12
[True, True, True, False]
0.09200620651245117


import numpy as np
import queue
import math
import time
w = [1,3,5,7]#体积
v = [2,4,6,8]#价值
N=4
begin = time.time()
def test(capacity):
vec_len = 2**(len(v)+1) - 1#树的节点个数
vec_v = np.zeros(vec_len)
vec_w = np.zeros(vec_len)
vec_w[0]=capacity
que = queue.Queue();
que.put(0)
best = 0
while(not que.empty()):
current = que.get()
level = int(math.log(current+1,2))
if(vec_v[current] > vec_v[best]):
best = current

left = 2*current+1#左子树索引
right = 2*current+2#右子树索引

if(left < vec_len and vec_w[current]-w[level] >= 0 ):
vec_v[left] = int(vec_v[current]+v[level])
vec_w[left] = vec_w[current]-w[level]
que.put(left)
if(right < vec_len and sum(v[level+1:N])+vec_v[current] >= vec_v[best]):
vec_v[right] = vec_v[current]
vec_w[right] = vec_w[current]
que.put(right)
print(vec_v[best])

test(10)
end = time.time()
print(end-begin)

12.0
0.0665130615234375


01-23 1599

02-22 188

04-27 112

08-07 199

09-18 1974

05-15 5756

11-25 23

04-26 309

02-27 4081

05-06 2311

04-03 152

06-14 4578

05-04 442

08-19 537

04-23 17

09-26 4746

04-08 46

04-14 820

09-13 5525

03-19 80万+

04-14 56万+

02-19 16万+

02-27 7万+

02-28 4万+

03-01 12万+

03-01 11万+

03-03 6201

03-04 12万+

03-05 9757

03-05 5万+

03-08 6万+

03-08 1万+

04-25 6万+

03-10 12万+

03-10 17万+

03-10 6381

03-12 10万+

03-13 10万+

03-16 1万+

03-19 7万+

03-20 5079

03-23 1万+

03-24 3万+

03-25 2万+

03-25 8万+

03-27 1万+

03-29 20万+

03-29 9万+

03-30 14万+

05-21 2934

03-31 1万+

05-25 5226

03-23 1万+

04-02 3万+

05-06 2万+

04-05 1万+

04-06 6万+

04-09 1万+

04-09 7万+

04-09 4722

04-10 4271

04-11 2万+

04-15 5万+

04-18 1万+

04-18 3798

04-18 4万+

04-20 3万+

04-24 2万+

04-26 3679

04-24 4149

04-30 7537

05-16 4万+

05-08 3万+

05-10 1923

05-11 3万+

05-13 6766

05-19 5692

05-13 9770

05-14 3676

python实用的几个脚本程序（自己在用）

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客