多重背包,附上例题(POJ - 1014 Dividing)
Posted 启动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多重背包,附上例题(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.
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.
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 }
以上是关于多重背包,附上例题(POJ - 1014 Dividing)的主要内容,如果未能解决你的问题,请参考以下文章