首页 > 代码库 > hdu 2191 悼念512汶川大地震遇难同胞——珍惜如今,感恩生活

hdu 2191 悼念512汶川大地震遇难同胞——珍惜如今,感恩生活

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,p[100+5],h[100+5],c[100+5],dp[100+5];

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,j;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
            scanf("%d%d%d",&p[i],&h[i],&c[i]);

        memset(dp,0,sizeof(dp));

        for(i=0;i<m;i++)
        {
            multiplepack(p[i],h[i],c[i]);
        }

        printf("%d\n",dp[n]);
    }
    return 0;
}
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

hdu 2191 悼念512汶川大地震遇难同胞——珍惜如今,感恩生活