HDU 1059 Dividing 多重背包

来源:互联网 发布:mysql between 日期 编辑:程序博客网 时间:2024/05/21 01:57

这道题的意思是,有六件物品每件物品的价值依次是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 01 0 0 0 1 10 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;}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 老干妈盖子太紧打不开怎么办 酵素桶变色了怎么办 酵素比例没弄好怎么办 离合器弹不起来怎么办 货车打不上气干燥器漏气怎么办 排气阀一直漏气怎么办 五个月宝宝长牙怎么办 宝宝不喜欢吃粥怎么办 孕晚期严重贫血怎么办 小孩子特别瘦该怎么办 胃病越来越瘦该怎么办 宝宝不愿吃米粉怎么办 米粉吃凉了怎么办 夏季给宝宝断奶怎么办 宝宝长牙牙痒怎么办 宝宝不喜欢吃肉怎么办 孩子不喜欢吃肉怎么办 小孩不喜欢吃肉怎么办 戒奶第二天很痛怎么办 宝宝不吃磨牙棒怎么办 金毛急性肠炎怎么办 狗狗病毒性肠炎怎么办 狗呕吐白色泡沫怎么办 甲减引起水肿怎么办 猫咪呕吐加拉稀怎么办 猫一直吐白沫怎么办 小猫吐白沫拉稀怎么办 大鳄不吃泥鳅怎么办 六个月宝宝不消化怎么办 两周岁宝宝拉稀怎么办 三岁宝宝拉肚子怎么办 混合痔老是出血怎么办 宝宝大便黑色怎么办呢 婴儿大便有粘液怎么办? 十个半月宝宝拉肚子怎么办 宝宝拉青粑粑怎么办 婴儿拉肚子带血怎么办 儿童拉肚子拉血怎么办 孩子拉肚子有血怎么办? 儿童大便带血怎么办 小孩拉痢疾发烧怎么办