zoukankan      html  css  js  c++  java
  • 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
    ",dp[N]);
        }
        return 0;
    }

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

  • 相关阅读:
    单据存储过程
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/xryz/p/4847849.html
Copyright © 2011-2022 走看看