1. 程式人生 > >POJ 1014 Dividing (多重揹包問題+遞迴)【模板】

POJ 1014 Dividing (多重揹包問題+遞迴)【模板】

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. 
InputEach 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. 
OutputFor 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.

 【題解】

 大致題意:

      有6種物品,每種物品的價值分別為1~6,現在輸入每種物品的個數,問能不能把這些物品分為等價的兩部分。

 分析:

     每件物品不一樣,能不能剛好分為總價值的一半,明顯的多重揹包問題,其特點就是物品數>=1, 所以現在就套用多重揹包模板。

遞迴的查詢能滿足條件的分類方式,如果找到就返回true,否則返回false,還有,既然是分兩堆,那麼如果總價值為奇數,就可以直接判定不可分了,

還有這道題比較卡剪枝,所以要適當剪枝,否則會TLE,具體見程式碼行註釋。

 【AC程式碼】

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[10];
int sum;
bool flag;

bool dfs(int mid,int sum,int i)//這裡mid是總價值的一半  sum是當前總價值  i表示當前選到價值為i的物品
{
    if(flag) return flag; //可以分為兩半
    if(mid == sum) //可以分為兩半
    {
        flag=true;
        return flag;
    }

    for(int j=i;j>=1;--j)//從當前價值開始遍歷
    {
        if(a[j]) //如果價值為i的物品還有剩餘
        {
            if(sum+j<=mid) //總價值還少於mid
            {
                a[j]--;//物品數減1

                dfs(mid,sum+j,j); //繼續尋找

                if(flag) //這裡相當於一個小小的剪枝, 能稍微提高一點效率 
                    break;
            }
        }
    }
    return flag;
}

int main()
{
    int n,p,cnt=1;
    while(1)
    {
        p=0;
        sum=0;
        for(int i=1;i<=6;++i)
        {
            scanf("%d",&a[i]);
            if(a[i]) p=1;
            sum+=a[i]*i; //總價值
        }
        if(p==0) break; //如果輸入全為0
        printf("Collection #%d:\n",cnt++);
        if(sum&1) //如果總數為奇數  直接判定不可分
        {
            printf("Can't be divided.\n\n");
            continue;
        }
        sum/=2; 
        flag=false;//預置為false
        bool tag=dfs(sum,0,6);
        if(tag) printf("Can be divided.\n");
        else printf("Can't be divided.\n");
        printf("\n");
    }
    return 0;
}