HDU1059:Dividing(多重背包二进制优化)

解决Marsha和Bill如何公平分配带有不同价值的大理石的问题,通过优化多重背包算法实现。

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24845    Accepted Submission(s): 7079


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.
 

Source

所谓多重背包就是01背包的升级版,每件物品有限定的件数,因此可以遍历每件物品的数目当01背包来做,但数据量大的时候必然超时,于是可以进行二进制优化,即将一定数目的物品集合拆分成若干份,每份的代价分别为2^0*原代价,2^1*原代价,2^2*原代价...2^n*原代价,价值亦然,比如某价值为10,重量为3的物品有7件,将这7件物品拆成重量分别为3(1*3),6(2*3),12(4*3),价值分别为10,20,40的三件物品,就可以优化时间了,这3,6,12重量的物品可以组成7件及以内的任意组合的重量, 所以不用担心漏掉组合的情况。

再如重量为1的物品有14件,拆成重量为1,2,4,7即可,虽然有重复,但也比原来优化了不少。

# include <stdio.h>
# define MAXN 120000
int v[7], n[7],value[2000],cost[2000],dp[MAXN+1];
int main()
{
    int w=1;
    while(1)
    {
        int sum = 0, icount=0;
        for(int i=1; i<=6; ++i)
        {
            scanf("%d",&n[i]);
            v[i] = i;
            sum += n[i]*i;
        }
        if(!sum)
            break;
        else if(sum&1)
            printf("Collection #%d:\nCan't be divided.\n",w++);
        else
        {
            sum >>= 1;
            for(int i=1; i<=6; ++i)
            {
                for(int j=1; j<=n[i]; j<<=1)
                {
                    cost[icount++] = j*v[i];
                    n[i] -= j;
                }
                if(n[i]>0)
                    cost[icount++] = n[i]*v[i];
            }
            for(int i=1; i<=MAXN; ++i)
                dp[i] = -0x3f3f3f3f;
            dp[0] = 0;
            for(int i=0; i<icount; ++i)
            {
                for(int j=sum; j>=cost[i]; --j)
                    if(dp[j-cost[i]] != -0x3f3f3f3f && dp[j-cost[i]]+cost[i] > dp[j])
                        dp[j] = dp[j-cost[i]]+cost[i];
            }
            if(dp[sum]==-0x3f3f3f3f)
                printf("Collection #%d:\nCan't be divided.\n",w++);
            else
                printf("Collection #%d:\nCan be divided.\n",w++);
        }
        printf("\n");
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值