2427: [HAOI2010] Software Installation time limit:10 Sec Memory limit:128 MB
submit:463 solved:194
[Submit] [Status] [Discuss] 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
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
An integer that represents the maximum value.
Sample INPUT3 10
5 5 6
2 3 4
0 1 1 Sample output5hint Source
Day2
After reading the question is clear: the first contraction ring, and then the tree-type DP, is probably a backpack. First F[i][j]=max (F[i][j],f[list[i]][k]+f[i][j-k]), J∈[0,w[i]],k∈[0,j]. Then F[i][j]=f[i][j-w[i]]+v[i],j∈[w[i],m] (because the subtree must be selected i).
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespacestd;intnext0[205],list0[205],head0[ the],next[205],list[205],head[ the];intn,m,top,top0,t,cnt,sum;BOOLvis[ the],inset[ the];intdfn[ the],low[ the],stack[ the],belong[ the],w[ the],w0[ the],v[ the],v0[ the];intf[ the][505];inlineintRead () {intA=0, f=1;CharC=GetChar (); while(c<'0'|| C>'9') {if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {a=a*Ten+c-'0'; C=GetChar ();} returnA *F;} InlinevoidInsert0 (intXinty) {next0[++top0]=Head0[x]; HEAD0[X]=top0; LIST0[TOP0]=y;} InlinevoidInsertintXinty) {Vis[y]=1; next[++top]=Head[x]; HEAD[X]=top; List[top]=y;}voidDfsintx) {Dfn[x]=low[x]=++sum; stack[++t]=x; INSET[X]=1; for(intI=head0[x];i;i=Next0[i]) { if(!Dfn[list0[i]]) {DFS (list0[i]); LOW[X]=min (Low[x],low[list0[i]]); } if(Inset[list0[i]]) low[x]=min (Low[x],dfn[list0[i]]); } inti=-1; if(dfn[x]==Low[x]) {CNT++; while(i!=x) {i=stack[t--]; Inset[i]=0; Belong[i]=CNT; W[CNT]+=W0[i]; V[CNT]+=V0[i]; }}}inlinevoidTarjan () { for(intI=1; i<=n;i++)if(!Dfn[i]) DFS (i);} Inlinevoidrebuild () { for(intI=1; i<=n;i++) for(intj=head0[i];j;j=Next0[j])if(belong[list0[j]]!=Belong[i]) insert (Belong[i],belong[list0[j]);}voiddpintx) { for(intI=head[x];i;i=Next[i]) {DP (list[i]); for(intj=m-w[x];j>=0; j--) for(intk=0; k<=j;k++) F[x][j]=max (f[x][j],f[x][k]+f[list[i]][j-K]); } for(intj=m;j>=0; j--) if(J>=w[x]) f[x][j]=f[x][j-w[x]]+v[x];Elsef[x][j]=0;}intMain () {n=read (); m=read (); for(intI=1; i<=n;i++) w0[i]=read (); for(intI=1; i<=n;i++) v0[i]=read (); for(intI=1; i<=n;i++) { intx=read (); if(x) insert0 (x,i); } Tarjan (); memset (Vis,0,sizeof(VIS)); Rebuild (); for(intI=1; i<=cnt;i++) if(!vis[i]) insert (cnt+1, i); DP (CNT+1); printf ("%d", f[cnt+1][m]); return 0;}
[HAOI2010] [BZOJ2427] Software Installation |tarjan|-Tree DP