0-1背包
procedure ZeroOnePack(cost,weight)
for v=V..cost
f[v]=max{f[v],f[v-cost]+weight}
完全背包
procedure CompletePack(cost,weight)
for v=cost..V
f[v]=max{f[v],f[v-c[i]]+w[i]}
多重背包(二进制优化)
procedure MultiplePack(cost,weight,amount)//重量、价值、数量
if cost*amount>=V
CompletePack(cost,weight)
return
integer k=1
while k<amount
ZeroOnePack(k*cost,k*weight)
amount=amount-k
k=k*2
ZeroOnePack(amount*cost,amount*weight)
在求解混合背包时可以根据不同的背包类型调用不同的程序。
背包问题求解代码
最新推荐文章于 2022-11-10 09:16:50 发布