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 ...
随机推荐
- js字符串转json
1,eval方式解析,这是最早的解析方式了.如下: 代码如下: function strToJson(str){ var json = eval('(' + str + ')'); return js ...
- 百度前端笔试题目--css 实现一个带尖角的正方形
今天在牛客网上看到这道题,发现自己并不会,看来自己css都没怎么学习,也不怎么会用.看了下答案,不是很明白,也在网上搜集了一些资料和解法,感觉一些同学博客上也写了一些解法和拓展,所以就在这里借鉴一下咯 ...
- C语言转义字符相关知识
在C语言里所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示.而C中定义了一些字母或数字前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称 ...
- C#结构函数与base关键字
//声明父类 class ProductsFather { public double Price { get; set; } public int Count { get; set; } publi ...
- Dom4j之xPath
XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...
- 关于本地$.get(url,function(data)),异步获取数据
起初 此处url为本地同目录下的html片段 $.get(url,function(data) { alert(url); $("#gallery").append(data); ...
- PHP扩展开发(3)-config.m4
1. 宏命令 1.1. dnl 注释 1.2. 扩展的工作方式 1.2.1) PHP_ARG_WITH不需要第三方库 1.2.2) PHP_ARG_E ...
- C#中睡眠函数Sleep
命名空间:using System.Threading; Thread.Sleep();//睡眠500毫秒,也就是0.5秒
- Java学习笔记--Collection和Collections的区别
转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别. 1.java.util.Collection 是一个集合 ...
- 最近用的几个sql语句
都在sqlserver数据库下操作,注意sqlserver与mysql和oracle的语法区别 用惯了mysql 和oracle,突然改用sqlserver,有诸多的不习惯,诸多的坑爹,好多的坑,一一 ...