HDU 1059 Dividing

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1059

多重背包,自己理解后,模板记在这了,背包九讲是好东西

#include <iostream>
using namespace std;
int dp[240480];

void ZeroOnPack(int cost,int weight,int V)
{
    for(int i=V;i>=cost;i--)
    {
        if(dp[i-cost])
            dp[i]=1;
    }
}

void CompletePack(int cost,int weight,int V)
{
    for(int i=cost;i<=V;i++)
    {
        if(dp[i-cost])
            dp[i]=1;
    }
}

void MultiplePack(int cost,int weight,int amount,int V)
{
    if(cost*amount>=V)
    {
        CompletePack(cost,weight,V);
        return;
    }
    int k=1;
    while(k<amount)
    {
        ZeroOnPack(cost*k,weight*k,V);
        amount=amount-k;
        k=k*2;
    }
    ZeroOnPack(cost*amount,weight*amount,V);
}
int main(int argc, const char *argv[])
{
    //freopen("input.txt","r",stdin);
    int marble[7];
    int T=1;

    while(true)
    {
        memset(marble,0,sizeof(marble));
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        int zero=0;
        int sum=0;
        for(int i=1;i<=6;i++)
        {
            cin>>marble[i];
            if(marble[i]==0)
                zero++;
            sum=sum+i*marble[i];
        }
        if(zero>=6) break;
        for(int i=1;i<=6;i++)
        {
            MultiplePack(i,i,marble[i],sum/2);
        /*    for (int j = 0; j <= sum/2; j++)
            {
                cout<<dp[j]<<" ";
            }
            cout<<endl;*/
        }
        printf("Collection #%d:\n",T);
        if(dp[sum/2]&&sum/2==sum-sum/2)
        {
            cout<<"Can be divided."<<endl;
        }
        else 
        {
            cout<<"Can't be divided."<<endl;
        }
        cout<<endl;
        ++T;
    }
    return 0;
}

 

posted @ 2013-09-21 21:44  Destino74  阅读(152)  评论(0编辑  收藏  举报