Codeforces 742D Arpa ' s weak amphitheater and Mehrdad ' s valuable hoses--(01 backpack variants)

Source: Internet
Author: User

  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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.