混合背包+多重背包的二进制优化
2013-10-27 21:40
465 查看
#include <iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define LL long long using namespace std; const int maxn = 200000+10; const int maxc = 1e9+7; LL f[maxn]; int v[210],w[210],m[210]; int n,V; void ZeroOnePack(int cost,int weight)//0-1背包 { for(int j=V;j>=cost;j--) f[j]=max(f[j],f[j-cost]+weight); } void CompletePack(int cost ,int weight)//完全背包 { for(int j=cost;j<=V;j++) f[j]=max(f[j],f[j-cost]+weight); } void MultiPack(int cost,int weight,int amount)//多重背包 { if(cost*amount>=V) { CompletePack(cost,weight); return; } int k=1; while(k<amount) { ZeroOnePack(k*cost,k*weight); amount-=k; k*=2; } ZeroOnePack(amount*cost,amount*weight); } int main() { cin>>n>>V; memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) scanf("%d %d %d",&v[i],&w[i],&m[i]); for(int i=1;i<=n;i++) { if(m[i]==1)//0-1背包 { ZeroOnePack(v[i],w[i]); } else if(m[i]==-1)//完全背包 { CompletePack(v[i],w[i]); } else //多重背包 { MultiPack(v[i],w[i],m[i]); } } cout<<f[V]<<endl; return 0; }
相关文章推荐
- 直接拿来用!10款实用Android UI工具
- [01分数规划]分数规划,最优比率生成树,最优比率生成环
- 在workstation上安装xenserver无法识别虚拟硬盘
- Android_ActionBar
- adb server is out of date.问题解决
- Delphi 19种反调试检测法
- 数据库主键选择
- Vijos P1443 银河英雄传说 并查集
- 基数排序
- 日历视图(CalendarView)组件的功能和用法
- python 实现ARP攻击
- Maze Exploration UVA784
- Zend Framework入门教程
- spring简介
- 初次学C语言的小成果
- 贪心算法java实现
- 续集一:
- main.cpp
- MFC控件位置调整和坐标确定
- SD卡的命令和返回值