Cogs 444. [HAOI2010] Software Installation

Source: Internet
Author: User

★★☆ input file: install.in output file: install.out Simple comparison
Time limit: 1 s memory limit: MB

"Problem description"
Now we have n software on hand, for a software I, it will occupy WI disk space, its value is VI. We want to choose from some software installed on a disk capacity of M computer, so that the value of these software as large as possible (that is, VI and the largest).
But now there is a problem: there is a dependency between software, that is, software I can work correctly only if software J (including Software J's direct or indirect dependency) is installed (software I relies on software j). Fortunately, one software relies on another software. If a software does not work properly, then it can play a role of 0.
We now know the dependencies between software: Software I relies on software di. Now, please design a solution to install the software as large as possible. A software can only be installed once, if a software is not dependent on the di=0, then as long as the software installed, it will work properly.

"Input Format"

Line 1th: N,m (0<=n<=100. 0<=M<=500)
Line 2nd: w1,w2,...,wi,...,wn (0<=wi<=m)
Line 3rd: V1,V2,...,VI,...,VN (0<=vi<=1000)
Line 4th: D1,d2,...,di,...,dn (0<=di<=n,di≠i)

"Output Format"

An integer that represents the maximum value.

"Input Sample"
3 10
5 5 6
2 3 4
0 1 1

"Output Example"
5

Exercises

  Depending on the dependency, you can draw a picture, there are three possible scenarios: 1. Dependencies form a tree 2. Dependencies constitute a ring 3. Dependencies form a ring under which a tree is hanging. Because there are 2, 3 of these conditions, so we have to tarjan pre-treatment, contraction ring for the point, re-build the map.

For the built diagram, run one side of the tree backpack, thinking similar to 01 backpack, F[x][tot] for the root of x, the capacity of TOT is the biggest benefit. Consider each subtree of X as an item, and then enumerate the capacity assigned to each subtree, and tot shifts from large to small.

Also a little, f[x][tot] to ensure that X to count in, the last processing can be guaranteed.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;Ten Const intmaxn= $, maxm= -; One intN,M,W[MAXN],V[MAXN],FA[MAXN]; A intVAL[MAXN],COST[MAXN],F[MAXN][MAXM]; -  -vector<int>TO[MAXN],SON[MAXN]; the intstac[maxn],top=0, Dfn[maxn],low[maxn],inkin[maxn],tot,index; - BOOLINSTAC[MAXN]; -vector<int>KIN[MAXN]; -InlinevoidTarjan (intx) { +dfn[x]=low[x]=index++; -stac[++top]=x; +instac[x]=true; A      for(intI=0; I<to[x].size (); i++){ at         inty=To[x][i]; -         if(dfn[y]==-1){ - Tarjan (y); -low[x]=min (low[x],low[y]); -         } -         Else if(instac[y]!=0){ inlow[x]=min (low[x],dfn[y]); -         } to     } +     if(dfn[x]==Low[x]) { -tot++; the         inty; *          Do{ $y=stac[top--];Panax Notoginsenginstac[y]=false; - kin[tot].push_back (y); theinkin[y]=tot; +} while(y!=x); A     } the } +InlinevoidCalcintx) { -     if(son[x].size () = =0){ $          for(inti=cost[x];i<=m;i++) f[x][i]=Val[x]; $         return ; -     } -      for(intI=0; I<son[x].size (); i++) Calc (son[x][i]); the      -      for(intI=0; I<son[x].size (); i++){Wuyi         inty=Son[x][i]; the          for(inttot=m;tot>=0; tot--){ -              for(intj=0; j<=tot;j++){ WuF[x][tot]=max (f[x][tot],f[x][tot-j]+f[y][j]); -             } About         } $     } -      for(inti=m;i>=0; i--){ -         if(I>=cost[x]) f[x][i]=f[x][i-cost[x]]+Val[x]; -         Elsef[x][i]=0; A     } + } the intMain () { -scanf"%d%d",&n,&M); $      for(intI=1; i<=n;i++) scanf ("%d",&w[i]); the      for(intI=1; i<=n;i++) scanf ("%d",&v[i]); the      for(intI=1; i<=n;i++){ thescanf"%d",&fa[i]); the To[fa[i]].push_back (i); -     } inmemset (dfn,-1,sizeof(DFN)); the      for(intI=1; i<=n;i++){ the         if(dfn[i]==-1) Tarjan (i); About     } the      for(intI=1; i<=tot;i++){ the         inty=kin[i][0]; the         if(Kin[i].size () >=2){//form a ring, take any one of them, and indent the ring as a point +Val[y]=v[y]; cost[y]=W[y]; -son[0].push_back (y); the              for(intj=1; J<kin[i].size (); j + +){Bayival[y]+=V[kin[i][j]]; thecost[y]+=W[kin[i][j]]; the             } -         } -         Else{//is a certain point in a tree, copied directly the             if(fa[y]==0)  theson[0].push_back (y); the             Else{ the                 intxx=Inkin[fa[y]]; -                 intyy=kin[xx][0]; the son[yy].push_back (y); the             } theVal[y]=v[y]; cost[y]=W[y];94         } the     } theval[0]=0; cost[0]=0; Calc0); theprintf"%d", f[0][m]);98     return 0; About}

Cogs 444. [HAOI2010] Software Installation

Related Article

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.