Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4501
Question:
There are a total of N items, each of which has a purchase price, purchase points, and score value.
There are K items available for purchase, with v1 yuan and V2 points. Ask the maximum score value.
Solution:
DP [I] [J] [k] indicates the opportunity to have I yuan, J points, and K free items, and the maximum score value.
There are four options for each item: 1. Buy with money 2. Use points for 3. Use a free item for 4. Do not use anything
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <queue> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; int DP [110] [110] [10]; // DP [I] [J] [k] indicates the maximum value struct thing {int A, B, Val ;} thing [110]; int main () {int N, V1, V2, K; while (scanf ("% d % D ", & N, & V1, & V2, & K )! = EOF) {int sum = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d ", & thing [I]. a, & thing [I]. b, & thing [I]. val); sum + = thing [I]. val;} memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) for (INT q = K; q> = 0; q --) for (Int J = V1; j> = 0; j --) for (int p = V2; P> = 0; p --) {If (j> = thing [I]. A & P> = thing [I]. b) // If you can use credit or money {DP [J] [p] [Q] = max (DP [J-thing [I]. a] [p] [Q] + thing [I]. val, DP [J] [p-thing [I]. b] [Q] + thing [I]. val), DP [J] [p] [Q]); If (q> = 1) // If you can use free quota DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else if (j <thing [I]. A & P <thing [I]. b) // If neither money nor points can be used {If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else if (j <thing [I]. a) // only points can be used {DP [J] [p] [Q] = max (DP [J] [p-thing [I]. b] [Q] + thing [I]. val, DP [J] [p] [Q]); If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val);} else // only pay {DP [J] [p] [Q] = max (DP [J-thing [I]. a] [p] [Q] + thing [I]. val, DP [J] [p] [Q]); If (q> = 1) DP [J] [p] [Q] = max (DP [J] [p] [Q], DP [J] [p] [q-1] + thing [I]. val) ;}} printf ("% d \ n", DP [V1] [V2] [k]);} return 0 ;}