hdu1059 Dividing (多重背包)
2015-11-12 22:12
429 查看
链接点我点我点我
简单多重背包
简单多重背包
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; int dp[120100],a[7]; int v,k; //v为体积 void ZeroOnePack(int cost,int weight) //weight为价值,cost为花费 { for(int i=v;i>=cost;i--) dp[i]=max(dp[i],dp[i-cost]+weight); } void CompletePack(int cost,int weight) { for(int i=cost;i<=v;i++) dp[i]=max(dp[i],dp[i-cost]+weight); } void MultipliesPack(int cost,int weight,int amount) { if(cost*amount>=v) CompletePack(cost,weight); else { for(k=1;k<amount;) { ZeroOnePack(k*cost,k*weight); amount-=k; k<<=1; } ZeroOnePack(amount*cost,amount*weight); } } int main() { int cas=0,sum; while(1) { cas++; sum=0; for(int i=1;i<=6;i++) { scanf("%d",&a[i]); sum+=a[i]*i; } if(!sum)break; if(sum%2) { printf("Collection #%d:\nCan't be divided.\n\n",cas); continue; } else { v=sum/2; memset(dp,0,sizeof(dp)); for(int i=1;i<=6;i++) MultipliesPack(i,i,a[i]); if(dp[v]==v) printf("Collection #%d:\nCan be divided.\n\n",cas); else printf("Collection #%d:\nCan't be divided.\n\n",cas); } } return 0; }
相关文章推荐
- nyoj 摆方格 1087 (数学规律)
- 多段图问题(图的邻接表)
- 使用MP4V2开源库将AAC打包到MP4文件中
- Going Home(最大匹配km算法)
- hdu 3951(博弈,成环)
- 农夫过河问题(图的邻接矩阵)
- 手游公司的冬天
- PHP开发调试环境配置(基于wampserver+Eclipse for PHP Developers )
- [kuangbin带你飞]专题一 简单搜索 I - Fire Game FZU 2150
- poj 1986 Distance Queries(LCA离线Tarjan算法)
- AngularJs 键盘事件和鼠标事件
- 图(邻接矩阵)的深度、广度优先遍历
- 没有躲过的坑--This function or variable may be unsafe.
- 【黑马程序员】C语言学习笔记(3)-预处理指令
- 没有躲过的坑--This function or variable may be unsafe.
- 习题3-9 UVa10340 All in All
- 修改oracle redo
- VS2010下 DLL的创建和使用总结
- android 系统回调onSizeChange, onMeasure, onLayout调用时机
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)