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.
The relationship between the software forms a tree or a ring, and the violence shrinks the ring to a point and then the tree-shaped backpack.
#include <cstdio>#include<vector>std::vector<int> ss[ -];intpa[ -];intid[ -];intw[ -],v[ -],fa;intn,m;intf[ -][ +];intg[ -][ +];voidMAXS (int&a,intb) { if(a<b) a=b;}intt=0;voidDfsintx) { intsz=ss[x].size (); if(SZ) { for(intI=0; i<sz;i++) DFS (Ss[x][i]); intu=ss[x][0]; for(intj=m;j>=0; j--) g[x][j]=F[u][j]; for(intI=1; i<sz;i++) {u=Ss[x][i]; for(intj=m;j>=0; j--) for(intk=0; k<=j;k++) Maxs (g[x][j],g[x][j-k]+F[u][k]); } for(intj=w[x];j<=m;j++) F[x][j]=g[x][j-w[x]]+V[x]; }Else{ for(inti=w[x];i<=m;i++) f[x][i]=V[x]; }}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) {scanf ("%d", w+i); } for(intI=1; i<=n;i++) {scanf ("%d", v+i); } for(intI=1; i<=n;i++) {scanf ("%d",&FA); if(I!=FA) pa[i]=FA; Id[i]=i; } for(intI=1; i<=n;i++){ intA=i; if(~Pa[i]) for(intj=1; j<= the; j + +){A=Pa[a]; if(a<1) Break; if(a==i) { for(a=pa[a];a!=i&&~a;a=FA) {v[i]+=V[a]; W[i]+=W[a]; FA=Pa[a]; Pa[a]=-1; Id[a]=i; } Pa[i]=0; Break; } } } for(intI=1; i<=n;i++){ if(~Pa[i]) ss[id[pa[i]]].push_back (i); } DFS (0);printf ("%d", f[0][m]); return 0;}
bzoj2427 Software Installation