HDOJ 2191
Ps:刷水, 背包水题哇卡卡, 易做图裸的多重背包直接模版........
code:
#include <stdio.h>
#include <string.h>
int n = 0, m = 0, dp[102];
void zero_one(int value, int weight)
{
int j = 0;
for(j = n; j>=value; j--)
dp[j] = dp[j]>dp[j-value]+weight? dp[j]:dp[j-value]+weight;
}
int main() www.zzzyk.com
{
int i = 0, j = 0, k = 0, t = 0;
int p[102], h[102], c[102];
scanf("%d",&t);
while(t--)
{
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++)
{
if(p[i]*c[i]>m)
for(j = p[i]; j<=n; j++)
dp[j] = dp[j]>dp[j-p[i]]+h[i]? dp[j]:dp[j-p[i]]+h[i];
else
{
k = 1;
while(k<c[i])
{
zero_one(k*p[i], k*h[i]);
c[i] -= k;
k *= 2;
}
zero_one(c[i]*p[i], c[i]*h[i]);
}
}
printf("%d\n", dp[n]);
}
return 0;
}
作者:ulquiorra0cifer
补充:软件开发 , C语言 ,