Considering that each primary part has only two attachments at most, we can solve the problem by converting the original problem to the 01 backpack problem. before using the 01 backpack, we need to process the input data, classify each item, that is, treat each main item and its accessories as a type of item. After processing, we can use the 01 backpack algorithm. When retrieving an item, we only need to obtain the largest one from the following four solutions: only the master part, Master part + Attachment 1, Master part + Attachment 2, Master part + Attachment 1 + Attachment 2. It is easy to get the following state transition equation:
F [I, j] = max {f [I-1, J],
F [I-1, J-A [I, 0] + A [I, 0] * B [I, 0],
F [I-1, J-A [I, 0]-A [I, 1] + A [I, 0] * B [I, 0] + A [I, 1] * B [I, 1],
F [I-1, J-A [I, 0]-A [I, 2] + A [I, 0] * B [I, 0] + A [I, 2] * B [I, 2],
F [I-1, J-A [I, 0]-A [I, 1]-A [I, 2] + A [I, 0] * B [I, 0] + A [I, 1] * B [I, 1] + A [I, 2] * B [I, 2]}
F [I, j] indicates that J yuan is used. The maximum value obtained for Class I items before the purchase. A [I, 0] indicates the price of the main parts of Category I items, A [I, 1] indicates the price of 1st accessories for category I items, a [I, 2] indicates the price of 2nd accessories for category I items, B [I, 0], B [I, 1], and B [I, 2] indicate the importance of the main component, 1st attachments, and 2nd attachments respectively.
# Include <iostream> using namespace STD; int ZF [65] [3], W [65] [3], V [65] [3], d [65] [3205]; int main () {int n, m, C, p, q, I, j, T; CIN> N> m; n/= 10; // It is an integer multiple of 10, so it can save space and time for (I = 1; I <= m; I ++) {CIN> C> P> q; C/= 10; // same as if (q = 0) {W [I] [Q] = C; V [I] [Q] = C * P;} else if (W [Q] [1] = 0) {W [Q] [1] = C; V [Q] [1] = C * P;} else {W [Q] [2] = C; V [Q] [2] = C * P ;}} for (I = 1; I <= m; I ++) for (j = 0; j <= N; j ++) {d [I] [J] = d [I-1] [J]; If (j> = W [I] [0]) {T = d [I-1] [J-W [I] [0] + V [I] [0]; If (T> d [I] [J]) d [I] [J] = T;} If (j> = W [I] [0] + W [I] [1]) {T = d [I-1] [J-W [I] [0]-W [I] [1] + V [I] [0] + V [I] [1]; if (T> d [I] [J]) d [I] [J] = T ;} if (j> = W [I] [0] + W [I] [2]) {T = d [I-1] [J-W [I] [0]-W [I] [2] + V [I] [0] + V [I] [2]; if (T> d [I] [J]) d [I] [J] = T ;} if (j> = W [I] [0] + W [I] [1] + W [I] [2]) {T = d [I-1] [J-W [I] [0]-W [I] [1]-W [I] [2] + V [I] [0] + V [I] [1] + V [I] [2]; if (T> d [I] [J]) d [I] [J] = T ;}} cout <D [m] [N] * 10 <Endl; return 0 ;}