Main topic:
A person is going to buy a gift, there is M yuan. There are n kinds of gifts, the value of each gift is WI, you buy the first I-piece gift k is can get Ai * k + Bi candy. Ask how you can get the most candy number. It's a complete backpack, actually. The number of items is more than one. dp[nth items] [already cost M-ary] DP formula: dp[n][m] = max (Dp[n-1][m-w[n]] + a[i]+b[i], Dp[n][m-w[i]] + a[i]); At the end of the day, move the result. for (i=0; i<=m;i++) dp[n][i] = max (Dp[n-1][i], dp[n][i]);
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>#include<cmath>using namespacestd;#defineINF 0X3FFFFFFFtypedef __int64 LL;ConstLL MAXN =2005;ConstLL mod = 1e9+7;intA[MAXN], B[MAXN], W[MAXN], dp[maxn][maxn*2];intT, N, M, W;intMain () {scanf ("%d", &T); while(t--) {scanf ("%d%d", &m, &N); for(intI=1; i<=n; i++) scanf (" %d%d%d", &w[i], &a[i], &B[i]); Memset (DP,-1,sizeof(DP)); dp[0][0] =0; intAns =0; for(intI=1; i<=n; i++) { for(intJ=w[i]; j<=m; J + +) { if(dp[i-1][j-w[i]]! =-1) Dp[i][j]= dp[i-1][j-w[i]] + a[i] +B[i]; if(Dp[i][j-w[i]]! =-1) Dp[i][j]= Max (Dp[i][j], Dp[i][j-w[i]] +A[i]); Ans=Max (ans, dp[i][j]); } for(intj=0; j<=m; J + +) Dp[i][j]= Max (Dp[i][j], dp[i-1][j]); } printf ("%d\n", ans); } return 0;}
HDU 5410 CRB and his Birthday