题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的。
解法:转化成多重背包
 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; int dp[];
int a[]; int nValue; //0-1背包,代价为cost,获得的价值为weight
void ZeroOnePack(int cost,int weight)
{
for(int i=nValue;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //完全背包,代价为cost,获得的价值为weight
void CompletePack(int cost,int weight)
{
for(int i=cost;i<=nValue;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //多重背包
void MultiplePack(int cost,int weight,int amount)
{
if(cost*amount>=nValue) CompletePack(cost,weight);
else
{
int k=;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=;
}
ZeroOnePack(amount*cost,amount*weight);//这个不要忘记了,经常掉了
}
} int main()
{
int iCase=;
while()
{
iCase++;
int tol=;
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
tol+=i*a[i];
}
if(tol==)break;
printf("Collection #%d:\n",iCase);
if(tol%!=)
{
printf("Can't be divided.\n\n");
continue;
}
else
{
nValue=tol/; }
memset(dp,,sizeof(dp));
for(int i=;i<=;i++)
MultiplePack(i,i,a[i]);
if(dp[nValue]==nValue)printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return ;
}

hdu 1059 多重背包的更多相关文章

  1. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

  2. Dividing (hdu 1059 多重背包)

    Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...

  3. hdu 1059 多重背包 背包指数分块

    思路: 这个方法要看<浅谈几类背包问题>这篇论文. #include"stdio.h" #define Max(a,b) (a)>(b)?(a):(b) ],k[ ...

  4. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  5. hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...

  6. Big Event in HDU(HDU 1171 多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. HDU1171--Big Event in HDU(多重背包)

    Big Event in HDU   Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  9. Big Event in HDU(多重背包套用模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...

随机推荐

  1. 工作者对象HttpWorkerRequest

    在ASP.NET中,用于处理的请求,需要封装为HttpWorkerRequest类型的对象.该类为抽象类,定义在命名空间System.Web下. #region Assembly System.Web ...

  2. ggplot绘图学习笔记

    0.查看R的系统帮助文档 标度 scale breaks, labels, limits, labs dose <- c(20, 30, 40, 45,60) drugA <- c(16, ...

  3. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  4. Forth scrum meeting - 2015/10/29

    今天下午,我们终于要到了MOOC服务器端开发人员的联系方式,于是我们小组派了三名同学去实验室与他们进行了交流,并咨询了他们一些关于API接口的问题.他们也对我们这个客户端的开发提出了一些建议. 开发团 ...

  5. 第11章 使用Vsftpd服务传输文件

    章节简述: 本章节先通过介绍文件传输协议来帮助读者理解FTP协议的用处,安装vsftpd服务程序并逐条分析服务文件的配置参数. 完整演示vsftpd服务匿名访问模式.本地用户模式及虚拟用户模式的配置方 ...

  6. Unity3D使用小技巧

    原地址:http://unity3d.9tech.cn/news/2014/0411/40178.html 1.Crtl+f摄像机自动适配场景. 2.可以用一个立方体作为底盘. 3.人物角色可以直接引 ...

  7. Public and Private Interfaces in ruby

    Your latest client is a bank, and they’ve tasked you with requiring customers to enter their passwor ...

  8. Unbuntu 下编译安装 PHP 必要库的安装

    2010/08/22 LINUX, PHP 2 COMMENTS 编译环境 sudo apt-get install build-essential xml sudo apt-get install ...

  9. HDU 1505 City Game (hdu1506 dp二维加强版)

    F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  10. 【云计算】Dockerfile、镜像、容器快速入门

    Dockerfile.镜像.容器快速入门 1.1.Dockerfile书写示例 Dockerfile可以用来生成Docker镜像,它明确的定义了Image的生成过程.虽然直接修改容器也可以提交生成镜像 ...