1 8 2 2 100 4 4 100 2
400
思路:最简单的一道多重背包模板题,根据状态方程
f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]}
很容易便能写出程序
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; struct node { int val,wei,num; } rice[500]; int dp[500][500]; int main() { int t,n,m,i,j,k,MAX,tem; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i = 0; i<m; i++) scanf("%d%d%d",&rice[i].val,&rice[i].wei,&rice[i].num); for(i = 0; i<=m; i++) { for(j = 0; j<=n; j++) { MAX = 0; for(k = 0; k<=rice[i].num && k*rice[i].val<=j; k++) { tem = dp[i-1][j-k*rice[i].val]+k*rice[i].wei; if(tem>MAX) MAX = tem; } dp[i][j] = MAX; } } printf("%d\n",dp[m][n]); } return 0; }