Poj-2486-apple Tree

Source: Internet
Author: User

Poj-2486-apple Tree

Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11210 Accepted: 3774

Description

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.

Input

There 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.

Output

For 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

Source

POJ Contest,author:[email protected]

17857578 2486 Accepted 892K 63MS g++ 1191B 2017-11-18 19:26:17

Tree-like DP.

The key point is to establish the DP transformation equation.

Dp[x][j][0] = max (dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0]);

Indicates that you are not going back to the current x point, the X current point of the j-t step, plus the step not to the Y point.


Dp[x][j][0] = max (dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1]);

Indicates that the current x point is not returned. is not back to the sum of the current X point and back to the y point.


DP[X][J][1] = max (dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1]);

Represents a return to the current X-point, which is the sum of the X-Points and the return to the child node Y.

poj-2486 #include <cstdio> #include <cstring> #include <iostream> #include <vector> using name Space Std; const int MAXN = 100 + 10; int N, K, VAL[MAXN], VIS[MAXN], dp[maxn][2*maxn][2]; Vector<int> VT[MAXN];  void Dfs (int x) {vis[x] = 1; for (int i=0; i<vt[x].size (); ++i) {int y = Vt[x][i], if (vis[y] = = 1) {continue;} dfs (Y); for (int j=k; j>=1;--j) {fo R (int t=1; t<=j; ++t) {dp[x][j][0] = max (dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0]); if (T >= 2) {dp[x][j][0] = max ( Dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1]); DP[X][J][1] = max (dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1]);  }}}}}int main () {freopen ("In.txt", "R", stdin); int A, b, ans;  while (scanf ("%d%d", &n, &k)! = EOF) {memset (DP, 0, sizeof (DP)), memset (Vis, 0, sizeof (VIS)); for (int i=1; i<=n; ++i) {scanf ("%d", &val[i]), for (int j=0; j<=k; ++j) {dp[i][j][0] = dp[i][j][1] = Val[i];}} for (int i=1; i<=n; ++i) {vt[i].clear ();} for (int i=1; i<n; ++i) {scanf ("%d%d", &a, &AMP;B); Vt[a].push_back (b); Vt[b].push_back (a); } dfs (1); ans = max (dp[1][k][0], dp[1][k][1]); printf ("%d\n", ans);} return 0; }

  

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.