POJ 2486 Apple Tree

Source: Internet
Author: User

                                                                                                                                               Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7120 Accepted: 2370

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


Although the topic is to look at the problem-solving report, but it is finally over. It's hard for me very very very long long time

Dp[a][b][0]: Node A go b step back to Node A

DP[A][B][1]: Node A step b does not go back to Node A


#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include < algorithm> #include <cstdlib> #include <queue> #include <set> #define N 210#define M 110using namespace std;struct num{int x,y,next;}    A[2*n];int b[n],top,n,m;int Dp[n][n][2],temp[n][n][2];int Main () {//freopen ("Data.txt", "R", stdin);    void addeage (int x,int y);    void Dfs (int u,int FA);        while (scanf ("%d%d", &n,&m)!=eof) {memset (dp,0,sizeof (DP));            for (int i=1;i<=n;i++) {int x;            scanf ("%d", &x);            Dp[i][0][0] = x;        DP[I][0][1] = x;        } memset (b,-1,sizeof (b));        Top = 0;            for (int i=1;i<=n-1;i++) {int x, y;            scanf ("%d%d", &x,&y);            Addeage (x, y);        Addeage (Y,X);        } dfs (1,-1);        int ans = 0;        for (int i=0;i<=m;i++) {ans = max (ans,dp[1][i][0]);        }printf ("%d\n", ans); } return 0;}    void addeage (int x,int y) {a[top].y = y;    A[top].next = B[x]; B[X] = top++;}        void Dfs (int u,int fa) {for (int i=b[u];i!=-1;i=a[i].next) {int v = a[i].y;        if (V==FA) {continue;        } dfs (V,U); for (int i=1;i<=n;i++) {for (int j=0;j<=m;j++) {temp[i][j][0] = Dp[i][j]                [0];            TEMP[I][J][1] = dp[i][j][1]; }} for (int i=1;i<=m;i++) {for (int j=1;j<=i;j++) {dp[u][                I][0] = max (dp[u][i][0],temp[u][i-j][1]+temp[v][j-1][0]);                    if (j>=2) {dp[u][i][0] = max (dp[u][i][0],temp[v][j-2][1]+temp[u][i-j][0]);                DP[U][I][1] = max (dp[u][i][1],temp[u][i-j][1]+temp[v][j-2][1]); }            }        }    }}


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.