多重背包,附上例题(POJ - 1014 Dividing)

直接上例题

Dividing

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 72067   Accepted: 18813

Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in 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

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 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

For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can‘t be divided.". 
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.
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 int cnt ;
 7 int dp[120004];
 8 void ZeroOne(int v, int w) {
 9     for(int i = cnt; i >= v ; i--){
10         dp[i] = max(dp[i],dp[i - v] + w);
11     }
12 }
13 void complete(int v, int w) {
14     for(int i = v; i <= cnt ; i++){
15         dp[i] = max(dp[i],dp[i - v] + w);
16     }
17 }
18 void multiply(int v,int w,int num) {
19     if(v*num < cnt){
20         for(int i = 1; i < num ; ){
21             ZeroOne(i*v,i*w);//二进制优化
22             num -= i;
23             i <<= 1;
24         }
25         ZeroOne(num*v,num*w);
26     }
27     else
28         complete(v,w);
29 }
30 int main()
31 {
32     int a[10];
33     int Case  = 1;
34
35     while( scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]) != EOF ) {
36         cnt = 0;
37         for(int i = 1; i <= 6; i++) {
38             cnt += a[i]*i;
39         }
40         if(!cnt) break;
41         printf("Collection #%d:\n",Case++);
42         if(cnt%2) { printf("Can‘t be divided.\n\n");continue;}
43         cnt /= 2;
44         memset(dp,0,sizeof(dp));
45         for(int i =1; i <= 6; i++) {
46             multiply(i,i,a[i]);
47         }
48         if(dp[cnt] == cnt) printf("Can be divided.\n\n");
49         else printf("Can‘t be divided.\n\n");
50     }
51     return 0;
52 }
时间: 2024-08-14 14:47:42

多重背包,附上例题(POJ - 1014 Dividing)的相关文章

【多重背包模板】poj 1014

#include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 using namespace std; int f[240005]; //f[j]相当于f[i][j]: 考虑1...i个物品,恰好放到容量为j,所能达到的最大价值 int v; //背包容量 void complete_pack(int *dp, int c, int w) { for(int i = c;

POJ 1014 Dividing (多重背包)

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 58921   Accepted: 15200 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl

POJ 1014 Dividing 背包

这道题使用多重背包,不过其实我也不太明白为什么叫这个名字. 因为感觉不是什么多重,而是物体的分解问题. 就是比如一个物体有数量限制,比如是13,那么就需要把这个物体分解为1, 2, 4, 6 如果这个物体有数量为25,那么就分解为1, 2, 4, 8, 10 看出规律吗,就是分成2的倍数加上位数,比如6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8,呵呵,为什么这么分解? 因为这样分解之后就可以组合成所有1到13的数,为25的时候可以组合成所有1到25的数啦

POJ 1014 Dividing(多重背包+二进制优化)

http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 8 const i

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

POJ 1014 Dividing(多重背包)

Dividing Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could

POJ 1014 Dividing【多重背包+二进制优化】

大意: 价值1, 2, 3, ……, 6的物品分别a1, a2, ……, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要看能不能将这些物品拼成   总价值 的 一半就可以了 转化为01背包是用二进制优化,否则会超时 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std;

POJ 1014: Dividing

写在前面: 本题非常有意思, 因此我在编程前后和撰写本文前, 在网上查询并阅读了大量关于本题的解题报告. 当中有两种似是而非但提交后却可以 AC 的解法引起了我的兴趣, 一是原作者宣称的 "DFS 解", 二是原作者宣称的 "扩展的找零问题解". 我对这两种解法的源码进行了细致的分析, 找到了它们的问题之所在. 以下我会先对这两种错误解法进行分析和讨论, 然后再给出正确的思路. 错误解法 1: 依照价值从大到小的顺序尝试凑数 (不回溯). 因为裁判的測试数据不够全面

0-1背包问题,附上例题(POJ - 3624 Charm Bracelet)

0-1背包问题的题目样式 有 N 件物品和一个容量为 M 的背包.放入第 i 件物品耗费的费用是 Wi,得到的价值是 Vi.求解将哪些物品装入背包可使价值总和最大. 0-1背包问题关键在于该物品放或不放,即在当前容量为M的的情况下,选择不选择该物品,那么就有一个转移方程 for(i=0  -  N) for(j=0  -  M) dp[i][j] = max(dp[i-1][j],dp[i-1][j+w[i]]+v[i]); 当前物品为i,当前的背包容量为j,如果不选当前该物品,则选取dp[i-