Test instructions: Give you a number of collections, each item in the collection is either selected, or all selected, to finally the maximum value in the range that the backpack can accommodate.
Analysis: For each and check set, from top to bottom rolling maintenance can, in fact, is a 01 backpack = =.
The code is as follows:
1#include <stdio.h>2#include <algorithm>3#include <string.h>4#include <vector>5 using namespacestd;6 Const intN = ++5;7 8 intW[n],b[n];9 intn,m,w;Ten intRoot[n]; Onevector<int>V[n]; A intAllw[n],allb[n]; - intDp[n]; - intFindintx) {returnx = = Root[x]? X:ROOT[X] =find (Root[x]);} the - intMain () - { -scanf"%d%d%d",&n,&m,&W); + for(intI=1; i<=n;i++) Root[i] =i; - for(intI=1; i<=n;i++) scanf ("%d", w+i); + for(intI=1; i<=n;i++) scanf ("%d", B +i); A for(intI=1; i<=m;i++) at { - intX,Y;SCANF ("%d%d",&x,&y); - intRx = Find (x), ry =find (y); - if(Rx! = ry) Root[rx] =ry; - } - for(intI=1; i<=n;i++) in { - intt =find (i); to V[t].push_back (i); +Allw[t] + =W[i]; -Allb[t] + =B[i]; the } * for(intI=1; i<=n;i++) $ {Panax Notoginseng if(v[i].size () = =0)Continue; - for(intj=w;j>=0; j--) the { + for(intk=0; K<v[i].size (); k++) A { the if(J-w[v[i][k]] >=0) Dp[j] = max (Dp[j], Dp[j-w[v[i][k]] +B[v[i][k]]); + } - if(J-allw[i] >=0) Dp[j] = max (Dp[j], Dp[j-allw[i]] +allb[i]); $ } $ } -printf"%d\n", Dp[w]); - return 0; the}
Codeforces 742D Arpa ' s weak amphitheater and Mehrdad ' s valuable hoses--(01 backpack variants)