背包不懂请先百度大牛的背包九讲,一开始可能看不懂,可以看一下:http://blog.youkuaiyun.com/cclsoft/article/details/4602734的帖子,把0-1理解了,后面的理解问题就不大了。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int mx = 200*20;
int dp[mx];
int cost[mx];
int heavy[mx];
int main()
{
// freopen("2191.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
memset(dp, 0, sizeof(dp));
int n,m;
scanf("%d%d",&n,&m);
int w = 0;
for(int i=1; i<=m; i++)
{
int p,h,c;
scanf("%d %d %d",&p,&h,&c);
for(int j=1; j<=c; j++)
{
cost[++w] = p;
heavy[w] = h;
}
}
for(int i=1; i<=w; i++)
{
for(int j=n; j>=1; j--)
{
if(j-cost[i]>=0 && dp[j - cost[i]] + heavy[i] > dp[j])
dp[j] = dp[j - cost[i]] + heavy[i];
}
}
printf("%d\n",dp[n]);
}
return 0;
}