POJ 2486 Apple Tree

Source: Internet
Author: User

Apple treetime limit:1000msmemory limit:65536kbthis problem'll be judged onPKU. Original id:2486
64-bit integer IO format: %lld Java class name: Main
Wshxzt is a lovely girl. She likes Apple very much. One day HX takes she to an apple tree. There is N nodes in the tree. Each node has a amount of apples. Wshxzt starts her happy trips at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn ' t allow wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes Apple very much. So she wants to eat as many as she can. Can many apples she can eat in to the most K steps.InputThere is several test cases in the input
Each test case contains three parts.
The first part is a numbers N K, whose meanings we have a talked about just now. We denote the nodes by 1 2 ... N. Since It is a tree with each node can reach any and other on only one route. (1<=n<=100, 0<=k<=200)
The second part contains N integers (all integers is nonnegative and not bigger than 1000). The ith number is the amount of apples in Node I.
The third part contains N-1 line. There is numbers a, A, a, and node B are adjacent, meaning.
Input would be ended by the end of file.

Note:wshxzt starts at Node 1.OutputFor each test case, output the maximal numbers of apples wshxzt can eat at a line.Sample Input
2 1 0 111 23 20 1 21 21 3
Sample Output
112
SourcePOJ Contest,author:[email protected]Problem solving: Tree-shaped DP
1#include <iostream>2#include <cstdio>3#include <vector>4#include <cstring>5 using namespacestd;6 Const intMAXN = $;7vector<int>G[MAXN];8 intdp[maxn][maxn][2],w[maxn],n,k;9 voidDfsintUintFA) {Ten      for(inti =0; I <= K; ++i) Onedp[u][i][0] = dp[u][i][1] =W[u]; A      for(inti = g[u].size ()-1; I >=0; --i) { -         if(G[u][i] = = FA)Continue; - DFS (g[u][i],u); the          for(intj = K; J >0; --j) { -              for(intK =1; K <= J; ++k) { -dp[u][j][1] = max (dp[u][j][1],dp[u][j-k][0] + dp[g[u][i]][k-1][1]); -                 if(k >=2) { +dp[u][j][1] = max (dp[u][j][1],dp[u][j-k][1] + dp[g[u][i]][k-2][0]); -dp[u][j][0] = max (dp[u][j][0],dp[u][j-k][0] + dp[g[u][i]][k-2][0]); +                 } A             } at         } -     } - } - intMain () { -     intu,v; -      while(~SCANF ("%d%d",&n,&K)) { in          for(inti =1; I <= N; ++i) scanf ("%d", w+i); -          for(inti =0; i < MAXN; ++i) g[i].clear (); to          for(inti =1; I < n; ++i) { +scanf"%d%d",&u,&v); - G[u].push_back (v); the g[v].push_back (u); *         } $Memset (DP,0,sizeofDP);Panax NotoginsengDfs1,-1); -printf"%d\n", Max (dp[1][k][0],dp[1][k][1])); the     } +     return 0; A}
View Code

POJ 2486 Apple Tree

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.