Topic Link: Click to open the link
The main topic: there is M money, there are n kinds of goods, each kind of goods have a WI price, suppose to buy x i in the goods, will spend wi*x money, at the same time the boss will also give A*x+b candy, ask how to buy gifts, can let candy number the most.
Dp[i][j][0] When I was bought, the money was J and I could get the most candy number without buying it.
DP[I][J][1] When I was bought, the money was J and bought the most candy I could get.
By the number of goods traversed from 1 to N, then you can omit one dimension, dp[i][0] in the current item, spend money for J not buy the current maximum value of this item, Dp[i][1] in the current item, spend money for J and buy the maximum value of the current item.
Then the state transfer equation has been
DP[I+W][1] = max (dp[i+w][1],dp[i][0]+a+b);
DP[I+W][1] = max (dp[i+w][1],dp[i][1]+a);
Note When updating an item dp[i] = MAX (dp[i][0],dp[i][1]); DP[I][1] is 0
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; int dp[2010][2]; int main () { int t, N, M, W, a, B, ans, I; scanf ("%d", &t); while (t--) { scanf ("%d%d", &m, &n); Ans = 0; Memset (Dp,0,sizeof (DP)); while (n--) { scanf ("%d%d%d", &w, &a, &b); for (i = 0; I <= m; i++) { if (i+w <= m) { dp[i+w][1] = max (dp[i+w][1],dp[i][0]+a+b); DP[I+W][1] = max (dp[i+w][1],dp[i][1]+a); } Dp[i][0] = max (dp[i][0],dp[i][1]); DP[I][1] = 0; ans = max (ans,dp[i][0]); } } printf ("%d\n", ans); } return 0;}
Copyright NOTICE: Reprint Please specify source: Http://blog.csdn.net/winddreams
Hdu5410 (more than 2015 schools)--CRB and his Birthday (knapsack problem)