背包问题模板
来源:互联网 发布:elect镜头数据 编辑:程序博客网 时间:2024/05/16 04:39
算法源于《背包九讲》
下面是实现代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<vector>#include<map>#include<stack>#include<list>#include<set>#include<queue>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1 | 1using namespace std;const int maxn=100005;int w[maxn],v[maxn],num[maxn];//代价数组和价值数组,以及相应的物品最大件数int W,n;//背包容量和物品件数int dp[maxn];//背包更新数组void ZeroOnePack(int weight,int value)//0-1背包{ for(int i=W;i>=weight;i--) dp[i]=max(dp[i],dp[i-weight]+value);}void CompletePack(int weight,int value)//完全背包{ for(int i=weight;i<=W;i++) dp[i]=max(dp[i],dp[i-weight]+value);}void MultiplePack(int weight,int value,int cnt)//多重背包{ if(weight*cnt>=W) CompletePack(weight,value); else { for(int k=1;k<cnt;k<<=1) ZeroOnePack(k*weight,k*value),cnt-=k; ZeroOnePack(cnt*weight,cnt*value); }}int main(){ //读入数据 return 0;}
0 0
- 部分背包问题模板
- 背包问题模板
- 背包问题_模板
- 简单背包问题模板
- 背包问题标准模板
- 背包问题模板
- 背包问题模板
- 背包问题模板
- 背包问题模板
- 01背包问题模板
- 多重背包问题模板
- 背包问题模板(01背包,完全背包,多重背包)
- 背包问题--0/1背包 【模板】
- 多重背包模板题 背包问题V2
- ACM_模板_背包问题
- 二维背包问题 + 代码模板
- 背包问题总结(模板)
- 多重背包问题(含模板)
- 兔子--Error generating final archive: Found duplicate file for APK: LICENSE
- ArrayList如何删除指定的几个数据?
- UVa129 - Krypton Factor
- 兔子--properties文件
- HaloDao进阶之面向sql动态视图的构思实现(三)
- 背包问题模板
- 优优云验证码识别积累一二
- android studio 下使用ndk——快速生成c/c++头文件
- HDU 2602 Bone Collector(0-1背包模板题)
- dll注入步骤
- [CF]Sums of Digits
- 兔子--runtime
- 小记2/12
- Power Strings