POJ-1155 TELE (tree-shaped dp+ Group backpack)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.