Title: To a tree with the right edge, each leaf node has the right. The Benquan represents the cost, and the weight of the leaf node represents how much it can compensate. Ask the maximum number of leaves that can be reached from the root node, making the total cost of paying no more than 0.
Title Analysis: The definition of State DP (U,K) represents the minimum cost of reaching k leaves from U. The state transition equation is:
DP (U,K) =min (DP (U,K), DP (SON,J) +DP (u,k-j) +u to son's cost).
PS: To add optimizations, otherwise time out.
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;const int n=3005;const int inf=1000000000;struct edge{int to,w,nxt;}; Edge e[n];int n,m,cnt;int w[n];int head[n];int dp[n][n];void Add (int u,int v,int W) {e[cnt].to=v;e[cnt].w=w;e[cnt].nxt= head[u];head[u]=cnt++;} void init () {int K,a,b;cnt=0;memset (head,-1,sizeof (head)), for (int i=1;i<=n-m;++i) {scanf ("%d", &k) and while (k--) {scanf ("%d%d", &a,&b); add (i,a,b);}} for (int i=n-m+1;i<=n;++i) scanf ("%d", w+i-n+m-1);} int dfs (int u) {for (int i=0;i<=m;++i) dp[u][i]=inf;dp[u][0]=0;if (u>n-m) {Dp[u][1]=-w[u-n+m-1];return 1;} int tot=0;for (int i=head[u];i!=-1;i=e[i].nxt) {int V=e[i].to;tot+=dfs (v); for (int. j=tot;j>=1;--j) for (int k=0;k< =j;++k) Dp[u][j]=min (Dp[u][j],e[i].w+dp[v][k]+dp[u][j-k]);} return tot;} void Solve () {DFS (1); int ans=0;for (int i=m;i>=0;--i) {if (dp[1][i]<=0) {ans=i;break;}} printf ("%d\n", ans);} int main () {while (~scanf ("%d%d", &n,&m)) {init (); Solve ();} RetUrn 0;}
POJ-1155 TELE (tree-shaped dp+ Group backpack)