https://www.lydsy.com/JudgeOnline/problem.php?id=2427
https://www.luogu.org/problemnew/show/P2515
Now we have N pieces of software at hand, for a piece of software i, it will occupy the disk space of Wi, and its value is Vi. We want to choose some software to install on a computer with a disk capacity of M, so that the value of these software is as large as possible (ie Vi's and Max).
But now there is a problem: there is a dependency between software, i.e. software i can only work correctly if software j (including direct or indirect dependencies of software j) is installed (software i depends on software j). Fortunately, at most one piece of software depends on one other piece of software. If a piece of software does not work properly, then it can play a role of 0.
We now know the dependencies between software: software i depends on software Di. Now please devise a plan to install the software with the greatest possible value. A piece of software can only be installed once. If a piece of software has no dependencies, Di=0. At this time, as long as the software is installed, it can work normally.
dp simple question, but debug for two days because the array is too small? ? ?
(But at the same time let me debug some bugs)
If the affiliation is a ring, obviously one of them must be chosen, and all the rings must be chosen, so tarjan shrinks and becomes a forest.
Build virtual points to connect each forest, and the rest is the backpack on the tree, which is the same as HDU1561: The more, The Better , but because n is very small, the method of O(n^2*m) is chosen.
At the same time, different from that question, because the volume can be 0, there may be a situation with after-effects, which is specially judged.
#include<map> #include<stack> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=105; const int M=505; inline int read(){ int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } struct node{ int to,nxt; } and [N * 2 ]; stack<int>q; bool inq[N]; int pre[N],d[N][N]; int cnt,head[N],n,m,dp[N][M]; int val[N],w[N],weight[N],b[N]; int dfn[N],low[N],to[N],indeg[N],t,l; inline void add(int u,int v){ e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt; } void dfs(int u){ for(int i=b[u];i<=m;i++)dp[u][i]=w[u]; for(int i=head[u];i;i=e[i].nxt){ int v=e[i].to; dfs(v); for(int j=m;j>=b[u];j--){ int tmp=dp[u][j]; for(int k=b[u];k<=j-b[v];k++){ if(k!=j)dp[u][j]=max(dp[u][j],dp[u][k]+dp[v][j-k]); else dp[u][j]=max(dp[u][j],tmp+dp[v][j-k]); } } } } void tarjan ( int u) { int v; dfn[u]=low[u]=++t; q.push(u);inq[u]=1; for(int i=head[u];i;i=e[i].nxt){ v=e[i].to; if(!dfn[v]){ Tarjan (v); low[u]=min(low[u],low[v]); }else if(inq[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]){ l++; do{ v=q.top();q.pop(); inq[v] = 0 ;to[v]= l; w[l]+=val[v];b[l]+=weight[v]; }while(v!=u); } } int main(){ n=read(),m=read(); for(int i=1;i<=n;i++)weight[i]=read(); for(int i=1;i<=n;i++)val[i]=read(); for(int v=1;v<=n;v++){ for [v] = read (); if (for [v]) add (for [v], v); } for(int i=1;i<=n;i++) if(!dfn[i])tarjan(i); memset(head,0,sizeof(head));cnt=0; for(int i=1;i<=n;i++){ int u=to[pre[i]],v=to[i]; if(!pre[i]||u==v)continue; if(!d[u][v]){ d[u][v]=1;add(u,v);indeg[v]++; } } int rt=l+1; for(int i=1;i<=l;i++) if(!indeg[i])add(rt,i); dfs(rt); printf("%d\n",dp[rt][m]); return 0; }
+++++++++++++++++++++++++++++++++++++++++++
+ Author of this article: luyouqi233. +
+Welcome to my blog: http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++