Topic links: https://www.luogu.com.cn/problem/P1064
01 backpack is a variant of the problem, but the situation may be different choice. For the backpack 01, for the i-th item, where there are two selection: select or not select, but the present situation there are five choice question: for a primary member, it may not be selected, only the main element may be selected, + may be selected primary first attachment member (if any), the master can also select a second attachment member + (if any), the master can also select a first attachment member + + second attachments (if any). Note that the only point numbers are continuous, the number does not work accessory, the main member number belongs to the number of ordered input, the cycle needs to determine whether it is a main member, since the main member must have.
code show as below:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 is #define Scand (X) Scanf ( "% LLF", & X) 14 #define F (I, A, B) for (int I = A; I <= B; I ++) 15 #define Scan (A) Scanf ( "% D", & A) 16 #define dbg (args) args COUT << << # ":" << endl << args; . 17 #define INF 0x3f3f3f3f 18 is #define MAXN 100005 . 19 int n-, m, T; 20 is int V [MAXN], W [MAXN], CNT [MAXN], WW [MAXN] [ . 5 ], VV [MAXN] [ . 5 ], DP [MAXN]; 21 is // CNT represents the number of the k-th primary attachment member , ww: right accessories weight vv: value annexes 22 int main () 23 { 24- //The freopen ( "P1064_5.txt", "R & lt", stdin); 25 // The freopen ( "output.txt", "W", stdout); 26 is STD :: :: iOS sync_with_stdio ( to false ); 27 Scan (m) ; 28 Scan (n-); 29 int Q, X, Y; 30 F (I, . 1 , n-) 31 is { 32 Scan (X); 33 is Scan (Y); 34 is Scan (Q); 35 IF (Q) 36 { 37 [ cnt [q] ++; // denotes the number i is the attachment of attachments of q cnt, at most two attachments 38 is WW [q] [cnt [q]] = X * Y; 39 VV [Q] [CNT [Q]] = X; 40 } 41 is the else 42 is { 43 is W [I] = X * Y; 44 is V [I] = X; 45 } 46 is 47 } 48 F (I, . 1 , n-) 49 for ( int J = m; J> = V [I] && V [I] =! 0 ; J,) // V [I] not equal to zero indicating the presence of the main member of the number of 50 { 51 is DP [J ] = max (DP [J], DP [JV [I]] + W [I]); 52 is IF (CNT [I]!) Continue ;//没有附件 53 if(cnt[i]>=1) 54 { 55 if(j-v[i]-vv[i][1]>=0) 56 dp[j]=max(dp[j],dp[j-v[i]-vv[i][1]]+w[i]+ww[i][1]); 57 } 58 if(cnt[i]>=2) 59 { 60 if(j-v[i]-vv[i][2]>=0) 61 { 62 dp[j]=max(dp[j],dp[j-v[i]-vv[i][2]]+w[i]+ww[i][2]); 63 } 64 if(j-v[i]-vv[i][1]-vv[i][2]>=0) 65 { 66 dp[j]=max(dp[j],dp[j-v[i]-vv[i][1]-vv[i][2]]+w[i]+ww[i][1]+ww[i][2]); 67 } 68 69 } 70 } 71 pf("%d",dp[m]); 72 }