转化为01背包问题,只是将原来的放或不放第i件物品改为放几件第i件物品。
#include<cstdio>
#include<cstring>
#define max(a, b) a>b?a:b
using namespace std ;
int dp[105] ;
struct page{
int weight ;
int value ;
int num ;
}pg[105] ;
int main(){
int t, n, m, i, j, k ;
scanf("%d", &t) ;
while(t--){
scanf("%d%d", &n, &m) ;
for(i=1; i<=m; i++)
scanf("%d%d%d", &pg[i].weight, &pg[i].value, &pg[i].num) ;
memset(dp, 0, sizeof(dp)) ;
for(i=1; i<=m; i++)
for(k=1; k<=pg[i].num; k++)
for(j=n; j>=pg[i].weight; j--)
dp[j] = max(dp[j], dp[j-pg[i].weight]+pg[i].value) ;
printf("%d\n", dp[n]) ;
}
return 0 ;
#include<cstring>
#define max(a, b) a>b?a:b
using namespace std ;
int dp[105] ;
struct page{
int weight ;
int value ;
int num ;
}pg[105] ;
int main(){
int t, n, m, i, j, k ;
scanf("%d", &t) ;
while(t--){
scanf("%d%d", &n, &m) ;
for(i=1; i<=m; i++)
scanf("%d%d%d", &pg[i].weight, &pg[i].value, &pg[i].num) ;
memset(dp, 0, sizeof(dp)) ;
for(i=1; i<=m; i++)
for(k=1; k<=pg[i].num; k++)
for(j=n; j>=pg[i].weight; j--)
dp[j] = max(dp[j], dp[j-pg[i].weight]+pg[i].value) ;
printf("%d\n", dp[n]) ;
}
return 0 ;
}