poj 1276 Cash Machine 多重背包

#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;

int dp[100000+5],N;

void zeroonepack(int cost,int weight)
{
    for(int i=N;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+weight);
}

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

void multiplepack(int cost,int weight,int amount)
{
    if(amount*cost>=N)
        completepack(cost,weight);
    else
    {
        int k=1;
        while(k<amount)
        {
            zeroonepack(cost*k,weight*k);
            amount-=k;
            k*=2;
        }
        zeroonepack(cost*amount,weight*amount);
    }
}

int main()
{
    int i,n,a[10],b[10];
    while(~scanf("%d%d",&N,&n))
    {
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++)
            scanf("%d%d",&a[i],&b[i]);
        for(i=0;i<n;i++)
            multiplepack(b[i],b[i],a[i]);
        printf("%d\n",dp[N]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-08-08 19:00  xryz  阅读(95)  评论(0编辑  收藏  举报