您的位置:首页 > 其它

HDU 1059 Dividing 多重背包

2013-09-06 20:44 357 查看
这道题的意思是,有六件物品每件物品的价值依次是1,2,3,4,5,6每件物品的个数各不相同。求所有物品价值是否能够平分,就是总价值的一半是否能由当前的物品的价值组合出来。所以这是一道多重背包的问题。状态转移方程是:

f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]} 枚举所有的情况,如果dp[w/2]可以找到的话那就满足条件、、


Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13377    Accepted Submission(s): 3754


Problem 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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 <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
int dp[101000], i, j, t;
int num[10], k, sum, case1 = 1;
while(cin >>num[1])
{
sum = num[1];
for(i = 2; i <= 6; i++)
{
cin >>num[i];
sum += num[i]*i;
}
if(sum == 0) break;
if(sum%2)
{
printf("Collection #%d:\nCan't be divided.\n\n",case1++);
continue;
}
t = sum/2;
for(i = 1; i <= t; i++)
dp[i] = 0;
for(i = 0; i <= num[1] && i <= t; i++)
dp[i] = 1;
for(i = 2; i <= 6; i++)
{
for(j = t; j >= 0; j--)
{
if(!dp[j])
continue;
for(k = 1; k <= num[i] && k <= t; k++)
{
if(dp[k*i+j])
break;
dp[k*i+j] = 1;
}
}
}
if(dp[t])
printf("Collection #%d:\nCan be divided.\n\n",case1++);
else
printf("Collection #%d:\nCan't be divided.\n\n",case1++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM dp