这道题和01背包问题唯一的区别是每种物品有多个,只要把s个相同物品当作s种来做就完全和01问题一样了.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include <iostream>
#include <sstream>
using namespace std;
struct Rice{
int p, h, c;
}r[105];
int dp[105];
int main()
{
// freopen("in.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++)
cin >> r[i].p >> r[i].h >> r[i].c;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < m; i++)
for(int j = 0; j < r[i].c; j++)
for(int h = n; h >= r[i].p; h--)
dp[h] = max(dp[h], dp[h - r[i].p] + r[i].h);
cout << dp[n] << endl;
}
return 0;
}