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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/