多重背包 poj 1014 --Dividing

                                                                                                Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 51221   Accepted: 12971

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.
题意:已知有六种价值的石子,计算能否将其分为价值相等的两部分。
解题思路:
假如总价值为奇数,则直接返回不能。
否则用多重背包检验看是否存在组合使得价值恰好为总价值的一半,如果存在,则可以将其分为价值相等的两部分,否则不能。
多重背包模板代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[10];
int dp[100000];
int vol;
void zeroone(int cost,int value)
{
       int v;
       for(v=vol;v>=cost;v--)
       dp[v]=max(dp[v],dp[v-cost]+value);
}
void complete(int cost,int value)
{
       int v;
       for(v=cost;v<=vol;v++)
       dp[v]=max(dp[v],dp[v-cost]+value);
}
void multiple(int cost,int value,int num)
{
       int k=1;
       if(cost*num>=vol)complete(cost,value);
       else
       {
              while(num-k>=0)
              {
                     zeroone(cost*k,value*k);
                     num-=k;
                     k=k*2;
              }
              zeroone(cost*num,value*num);
       }
}
int main()
{
       int i,j,k,m,n,T,t=0;
       while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6])
       {
              vol=a[1]+2*a[2]+3*a[3]+4*a[4]+5*a[5]+6*a[6];
              if(vol==0)break;
              printf("Collection #%d:\n",++t);
              if(vol%2==1)
              {
                     puts("Can't be divided.\n");continue;
              }
              vol/=2;
              memset(dp,0,sizeof(dp));
              for(i=1;i<=6;i++)multiple(i,i,a[i]);
              if(dp[vol]==vol)puts("Can be divided.");
              else puts("Can't be divided.");
              puts("");
       }
       return 0;
}


posted @ 2013-05-26 18:53  线性无关  阅读(136)  评论(0)    收藏  举报