poj1014 Dividing (多重背包)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接: id=1014">http://poj.org/problem?id=1014
Description
half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the
same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot
be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided. Collection #2:
Can be divided.
Source
大致题意:
有分别价值为1,2,3,4,5,6的6种物品,输入6个数字。表示对应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等。当中一个物品不能切开,仅仅能分给当中的某一方,当输入六个0是(即没有物品了),这程序结束。总物品的总个数不超过20000
输出:每一个測试用例占三行:
第一行: Collection #k: k为第几组測试用例
第二行:能否分(详细形式见用例)
第三行:空白(必须注意,否则PE)
#include <cstdio>
#include <iostream>
#define INF 100000000
using namespace std; int f[240005]; //f[j]相当于f[i][j]: 考虑1...i个物品。恰好放到容量为j。所能达到的最大价值
int v; //背包容量
int max(int a,int b)
{
if(a > b)
return a;
return b;
}
//处理一个全然背包 (该种物品不限量)
void complete_pack(int *a, int c, int w)
{
for(int i = c; i <= v; i++)
a[i] = max(a[i], a[i - c] + w);
} //处理一个 01背包 (该种物品仅仅有一个)
void zeroone_pack(int *a, int c, int w)
{
for(int i = v; i >= c; i--)
a[i] = max(a[i], a[i - c] + w);
} //处理一个多重背包 (该种物品指定上限)
void mutiple_pack(int *a, int c, int w, int m)
{
//若该种物品足以塞满背包-->转化为全然背包
if(c * m >= v)
{
complete_pack(a, c, w);
return;
} /*二进制思想拆分:多重背包中的一个物品--变成-->0-1背包中的多个物品
容量:2^0 2^1 2^2 2^k m-∑前面 ***保证k达到最大值
价值:2^0*c 2^1*c 2^2*c 2^k*c (m-∑前面 )*c
*/
int k = 1;
while(k <= m)
{
zeroone_pack(a, k * c, k * w);
m = m - k;
k = 2 * k;
}
} int main()
{
//freopen("d:/data.in","r",stdin);
//freopen("d:/data.out","w",stdout);
int sum, i, c[7], w[7], m[7],cas = 0;
while(scanf("%d%d%d%d%d%d", &m[1], &m[2], &m[3], &m[4], &m[5], &m[6]))
{
if(m[1] == 0 && m[2] == 0 && m[3] == 0 && m[4] == 0 && m[5] == 0 && m[6] == 0)
break;
sum = 0;
for(i = 1; i <= 6; i++)
{
c[i] = w[i] = i;
sum += c[i] * m[i];
}
printf("Collection #%d:\n", ++cas);
if(sum %2 == 1)
{
printf("Can't be divided.\n\n");
}
else
{
sum /= 2;
v = sum;
for(i = 1; i <= sum; i++)
f[i] = -INF;
f[0] = 0; for(i = 1; i <= 6; i++)
mutiple_pack(f, c[i], w[i], m[i]);
if(f[v] != v)//事实上仅仅要f[v]有正值就能够
{
printf("Can't be divided.\n\n");
}
else
{
printf("Can be divided.\n\n");
}
}
}
return 0;
}
poj1014 Dividing (多重背包)的更多相关文章
- POJ1014(多重背包)
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65044 Accepted: 16884 Descri ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- poj1014 dp 多重背包
//Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Dividing 多重背包 倍增DP
Dividing 给出n个物品的价值和数量,问是否能够平分.
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
随机推荐
- [Angular 2] Transclusion in Angular 2
Link: Blog Single transclude: <ng-content></ng-content> Multi-translcude: <ng-content ...
- Hadoop-2.2.0中文文档—— Common - CLI MiniCluster
目的 使用 CLI MiniCluster, 用户能够简单地仅仅用一个命令就启动或关闭一个单一节点的Hadoop集群,不须要设置不论什么环境变量或管理配置文件. CLI MiniCluster 同一时 ...
- jquery源码阅读笔记一
1. jquery无new的构造函数. 无new的构造函数是怎么实现的.比如我们一般这么用jQuery. $(".test").text(); 但是我们一般是这么写的. var t ...
- (转)sp_executesql介绍和使用
execute相信大家都用的用熟了,简写为exec,除了用来执行存储过程,一般都用来执行动态Sql sp_executesql,sql2005中引入的新的系统存储过程,也是用来处理动态sql的, 如: ...
- 纯js滑动脚本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 关于在Java代码中写Sql语句需要注意的问题
最近做程序,时不时需要自己去手动将sql语句直接写入到Java代码中,写入sql语句时,需要注意几个小问题. 先看我之前写的几句简单的sql语句,自以为没有问题,但是编译直接报错. String st ...
- swt
http://blog.sina.com.cn/s/blog_557ebb4c0101mgtc.html http://blog.csdn.net/kagoy/article/details/1746 ...
- XP 安装
提供一下裝系統的詳細步驟,盡量詳細到每一步都有,希望能對樓主有所幫助,不盡之處還請樓主不吝指出!謝謝 装XP的步骤如下: 开机时,按del键, 进入bios界面,一般选左侧第二项,(Advanced ...
- 文成小盆友python-num8 面向对象中的成员,成员修饰符,特殊成员,异常处理,设计模式之单例模式
本节主要内容: 1.面向对象中的成员 2.成员修饰符 3.特殊成员 4.异常处理 5.设计模式之单例模式 一.面向对象中的成员(类的成员) 类的成员总共可以分为3大类,每类中有不同的分支. 1.总述, ...
- Python 升级
1.到官网下载对于的版本: 2.下载之后并解压出来,编译: tar xf python.xx.xx.tar.xz sudo mkdir /usr/local/python ./configure -- ...