多重背包转换成完全背包和01背包
来源:互联网 发布:python 参数 编辑:程序博客网 时间:2024/05/22 13:15
void CompletePack(int cost,int weight) 多重背包
{
for(int i=cost;i<=m;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
void ZeroOnePack(int cost,int weight) 01背包
{
for(int i=m;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
void MultiplyPack(int cost,int weight,int amount) 完全背包
{
if(cost*amount>=m)
CompletePack(cost,weight);
else
{
int k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;
}
ZeroOnePack(amount*cost,amount*weight);
}
{
for(int i=cost;i<=m;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
void ZeroOnePack(int cost,int weight) 01背包
{
for(int i=m;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
void MultiplyPack(int cost,int weight,int amount) 完全背包
{
if(cost*amount>=m)
CompletePack(cost,weight);
else
{
int k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
详情看
HDU 2844 Coins (动规)
0 0
- 多重背包转换成完全背包和01背包
- 背包问题之01背包、完全背包和多重背包
- 01背包,完全背包, 多重背包
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包详解
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包模板
- 01背包 完全背包 多重背包
- 01背包,完全背包,多重背包详解
- 01背包 完全背包 多重背包
- 01背包、完全背包、多重背包模版。
- 01背包&完全背包&多重背包 - 区别
- 01背包、完全背包、多重背包归纳
- 01背包 完全背包 多重背包
- WAS7升级后遗症
- 将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。
- Effective Java (7) 避免使用终结方法
- 图像分割—基于图的图像分割(OpenCV源码注解)
- 261页binaryMaxIndexSmallerThanValue
- 多重背包转换成完全背包和01背包
- HOJ 12822 The Alphabet Sticker
- bzoj1690 [Usaco2007 Dec]奶牛的旅行
- C语言,求数组中第二大值,两种方法.
- hdoj 1061 Rightmost Digit 【数论】
- NSInvocation在获取返回值后crash问题
- 【代码优化】方法签名及参数检查
- 如何快速启动Android模拟器(Android Emulator)
- 【coding看计算机系统s】链接器