void ZeroOnePack(int cost,int weight)
{
int v;
for(v=V;v>=cost;v--) f[v]=max(f[v],f[v-cost]+weight);
}
void CompletePack(int cost,int weight)
{
int v;
for(v=cost;v<=V;v++)
f[v]=max(f[v],f[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount)
{
int k;
if(cost*amount>=V)
{
CompletePack(cost,weight);
return;
}
k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount=amount-k;
k=k*2;
}
ZeroOnePack(amount*cost,amount*weight);
}
//memset(f,0,sizeof(f[0])*(V+1)); // 只希望价格尽量大
//memset(f,-M,sizeof(f[0])*(V+1));f[0]=0; // 要求恰好装满背包
01 完全 多重 背包模板
最新推荐文章于 2021-08-08 19:48:40 发布