POJ 1014 Dividing (多重揹包)

Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 58921   Accepted: 15200

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.

Source

Mid-Central European Regional Contest 1999

題目大意:

有六個大理石,他們的價值分別是1,2,3,4,5,6,然後分別給出六個大理石的個數,問如何平分給兩個人,令兩個人所得到的價值相等。

這裏要求恰好達到平分這個狀態,也就是當揹包大小是價值的一半恰好裝滿,所以dp的初始值是負無窮。
開始以爲數據小(沒看清題),其實大的該死,我就用01揹包去做,果斷超時。
所以就改成多重揹包改二進制優化的01揹包+完全揹包。就是個模板了。好像只要優化的01揹包也能過,不過不知道怎麼做。
 下面是模擬多重揹包問題,總共6個物品,將第i個物品的價值與費用都看做是i,這樣套用揹包九講的多重揹包模板就可以了,不過注意的是數組要開的大一點.

代碼:16MS (這是codeblocks運行的代碼)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define M 100005
#define INF -99999;
int sum,v[7],dp[M];
void CompletePack(int cost,int wight)
{
    int i;
    for(i=cost;i<=sum;i++)
        dp[i]=max(dp[i],dp[i-cost]+wight);
}
void ZeroOnePack(int cost,int wight)
{
    int i;
    for(i=sum;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+wight);
}
void MultiplePack(int cost,int wight,int amount)
{
    if(cost*amount>=sum)
    {
        CompletePack(cost,wight);
        return;
    }
    int k=1;
    while(k<amount)
    {
        ZeroOnePack(k*cost,k*wight);
        amount-=k;
        k=k<<1;
    }
    ZeroOnePack(amount*cost,amount*wight);
}
void DP()
{
    int i,j,k;
    for(i=0;i<=sum;i++) dp[i]=i==0?0:INF;
    for(i=1;i<=6;i++)
        MultiplePack(i,i,v[i]);
}
int main()
{
    int flag,i,t=0;
    while(1)
    {   t++;sum=0;
        for(i=1;i<=6;i++)
    {
        cin>>v[i];
        sum+=i*v[i];
    }
    if(!sum) break;
    printf("Collection #%d:\n",t);
        if(sum&1){
            printf("Can't be divided.\n\n");
            continue;
        }
        sum=sum>>1;
        DP();
        if(dp[sum]>=0)
            printf("Can be divided.\n\n");
        else
            printf("Can't be divided.\n\n");
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章