hdu1059 Dividing (多重背包)

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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
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;k<amount;)
        {
            ZeroOnePack(k*cost,k*weight);
            amount-=k;
            k<<=1;
        }
        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;
}

你可能感兴趣的:(hdu1059 Dividing (多重背包))