There are n brands, and each brand must have at least one.
01 backpack with one-dimensional, design status DP [k] [I] indicates the maximum value of the first K spent I, the status can be obtained by the former K-1 and the current brand, pay attention to the initialization and equation location (no aftereffect ).
View code
1 /* 2 Author: zhaofa Fang 3 Lang: C ++ 4 */ 5 # Include <cstdio> 6 # Include <cstdlib> 7 # Include <sstream> 8 # Include <iostream> 9 # Include <cmath> 10 # Include <cstring> 11 # Include <algorithm> 12 # Include < String > 13 # Include <utility> 14 # Include <vector> 15 # Include <queue> 16 # Include <stack> 17 # Include <map> 18 # Include < Set > 19 Using Namespace STD; 20 21 Typedef Long Long Ll; 22 # Define Debug (x) cout <# x <':' <x <Endl 23 # Define Rep (I, n) for (INT I = 0; I <(n); I ++)24 # Define For (I, S, T) for (INT I = (s); I <= (t); I ++) 25 # Define Ford (I, S, T) for (INT I = (s); I> = (t); I --) 26 # Define PII pair <int, int> 27 # Define PB push_back 28 # Define MP make_pair 29 # Define FT first 30 # Define SD second 31 # Define Lowbit (x) (X & (-x )) 32 # Define INF (1 <30) 33 34 Int DP [ 11 ] [ 10005 ]; 35 Vector < Int > P [ 11 ], V [11 ]; 36 Int Main () 37 { 38 // Freopen ("in", "r", stdin ); 39 // Freopen ("out", "W", stdout ); 40 Int N, m, K; 41 While (~ Scanf ( " % D " , & N, & M ,& K )) 42 { 43 Int A, B, C; 44 Rep (I, 11 ) P [I]. Clear (), V [I]. Clear (); 45 Rep (I, n) 46 { 47 Scanf ( " % D " , & A, & B ,& C ); 48 P [A]. Pb (B); V [A]. Pb (C ); 49 } 50 Memset (DP ,- 1 , Sizeof (DP )); 51 Rep (I, m + 1 ) DP [ 0 ] [I] = 0 ; 52 For (K, 1 , K) 53 { 54 Rep (J, V [K]. Size ()) 55 { 56 Ford (I, m, p [k] [J]) 57 { 58 If (DP [k] [I-P [k] [J]! =- 1 ) // If the two equations exchange positions, the non-aftereffect principle of DP is not met, which can be known from the second group of data. 59 DP [k] [I] = max (DP [k] [I], DP [k] [I-P [k] [J] + V [k] [J]); 60 61 If (DP [k- 1 ] [I-P [k] [J]! =- 1 ) 62 DP [k] [I] = max (DP [k] [I], DP [k- 1 ] [I-P [k] [J] + V [k] [J]); 63 64 // Debug (K), debug (I), debug (DP [k] [I]); 65 } 66 } 67 } 68 Int Ans =- 1 ; 69 For (J, 0 , M) ans = Max (ANS, DP [k] [J]); 70 If (ANS! =- 1 ) Printf ( " % D \ n " , ANS ); 71 Else Puts (" Impossible " ); 72 } 73 Return 0 ; 74 } 75 /* 76 3 5 3 77 1 2 5 78 2 2 1 79 3 2 2 80 81 3 5 3 82 1 0 5 83 2 0 1 84 3 0 2 85 86 5 10000 3 87 1 4 6 88 2 5 7 89 3 4 99 90 1 55 77 91 2 44 66 92 Ans: 93 Impossible 94 8 95 255 96 */