hdu1059 Dividing (多重背包)

链接点我点我点我
简单多重背包

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int dp[120100],a[7];
int v,k;  //v为体积

void ZeroOnePack(int cost,int weight)   //weight为价值,cost为花费
{
    for(int i=v;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+weight);
}

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

void MultipliesPack(int cost,int weight,int amount)
{
    if(cost*amount>=v)
        CompletePack(cost,weight);
    else
    {
        for(k=1;k1;
        }
        ZeroOnePack(amount*cost,amount*weight);
    }
}


int main()
{
    int cas=0,sum;
    while(1)
    {
        cas++;
        sum=0;
        for(int i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i]*i;
        }
        if(!sum)break;
        if(sum%2)
        {
            printf("Collection #%d:\nCan't be divided.\n\n",cas);
            continue;
        }
        else
        {
            v=sum/2;
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=6;i++)
                MultipliesPack(i,i,a[i]);
            if(dp[v]==v)
              printf("Collection #%d:\nCan be divided.\n\n",cas);
            else printf("Collection #%d:\nCan't be divided.\n\n",cas);
        }
    }

    return 0;
}

你可能感兴趣的:(~~~~~acm~~~~~,===背包===,多重背包)