思路:简单动态规划,多重背包转化成0 1背包问题
#include<stdio.h>
#include<string.h>
int a[101][2001],rcw[2001],rcp[2001];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int C,i,j,k,pg,n,m,pr,w;
scanf("%d",&C);
while(C--)
{
memset(a,0,sizeof(a));
k = 0;
scanf("%d%d",&n,&m);
for(i = 0;i < m;i ++)
{
scanf("%d%d%d",&pr,&w,&pg);
for(j = k;j < k+pg;j ++)
{
rcw[j] = w;
rcp[j] = pr;
}
k = j;
}
for(i = 1;i <= n;i ++)
{
for(j = 0;j < k;j ++)
{
if(i>=rcp[j])
a[i][j] = max(a[i][j-1],a[i-rcp[j]][j-1]+rcw[j]);
else
a[i][j] = a[i][j-1];
}
}
printf("%d
",a[n][k-1]);
}
return 0;
}