bzoj 2427: [HAOI2010]软件安装
额,(sb题??)
缩了点搞呗,,(背包。。。)
1 #include<cstdio> 2 #include<algorithm> 3 #define N 100005 4 #define eps 1e-8 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 int n,m,cnt,sc,ind,top,CNT; 14 int v[105],w[105]; 15 int sv[105],sw[105]; 16 int dfn[105],low[105],belong[105]; 17 int q[105],f[105][505],in[505]; 18 struct edge{ 19 int to,next; 20 }e[505],E[505]; 21 int HEAD[105],head[105]; 22 bool inq[105]; 23 void insert(int x, int y) 24 { 25 e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt; 26 } 27 void INSERT(int x, int y) 28 { 29 in[y]=1; 30 E[++CNT].to=y; E[CNT].next=HEAD[x]; HEAD[x]=CNT; 31 } 32 void tarjan(int x) 33 { 34 int now=0; 35 low[x]=dfn[x]=++ind; 36 q[++top]=x; inq[x]=1; 37 for (int i=head[x];i;i=e[i].next) 38 if (!dfn[e[i].to]) 39 { 40 tarjan(e[i].to); 41 low[x]=min(low[x],low[e[i].to]); 42 } 43 else if (inq[e[i].to]) low[x]=min(low[x],dfn[e[i].to]); 44 if (low[x]==dfn[x]) 45 { 46 sc++; 47 while (now!=x) 48 { 49 now=q[top--]; inq[now]=0; 50 belong[now]=sc; 51 sv[sc]+=v[now]; 52 sw[sc]+=w[now]; 53 } 54 } 55 } 56 void rebuild() 57 { 58 for (int x=1; x<=n; x++) 59 for (int i=head[x];i;i=e[i].next) 60 if (belong[e[i].to]!=belong[x]) 61 INSERT(belong[x],belong[e[i].to]); 62 } 63 void dp(int x) 64 { 65 for (int i=HEAD[x];i;i=E[i].next) 66 { 67 dp(E[i].to); 68 for (int j=m-sw[x]; j>=0; j--) 69 for (int k=0; k<=j; k++) 70 f[x][j]=max(f[x][j],f[x][k]+f[E[i].to][j-k]); 71 } 72 for (int j=m; j>=0; j--) 73 if (j>=sw[x]) f[x][j]=f[x][j-sw[x]]+sv[x]; 74 else f[x][j]=0; 75 } 76 int main() 77 { 78 n=ra(); m=ra(); 79 for (int i=1; i<=n; i++) w[i]=ra(); 80 for (int i=1; i<=n; i++) v[i]=ra(); 81 for (int i=1; i<=n; i++) insert(ra(),i); 82 for (int i=1; i<=n; i++) 83 if (!dfn[i]) tarjan(i); 84 rebuild(); 85 for (int i=1; i<=sc; i++) 86 if (!in[i]) INSERT(sc+1,i); 87 dp(sc+1); 88 printf("%d\n",f[sc+1][m]); 89 return 0; 90 }