POJ 2486 Apple Tree

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2486 Apple Tree相关的知识,希望对你有一定的参考价值。

                                                                                                                                        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 her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip 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 you tell how many apples she can eat in at most K steps.

Input

There are several test cases in the input
Each test case contains three parts.
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)
The second part contains N integers (All integers are 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 are two numbers A,B in each line, meaning that Node A and Node B are adjacent.
Input will 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 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

11
2

Source


尽管这个题目是看的解题报告。可是还是最终过了。这题真是难为了我very very very long long time time

dp[a][b][0]:节点a 走b 步回到节点a

dp[a][b][1]: 节点a 走 b 步不回到节点a


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <set>
#define N 210
#define M 110
using 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]);
                }
            }
        }
    }
}













以上是关于POJ 2486 Apple Tree的主要内容,如果未能解决你的问题,请参考以下文章

POJ2486 Apple Tree

POJ 2486 Apple Tree (树形DP)

poj2486Apple Tree[树形背包!!!]

POJ 2486 Apple Tree

POJ 2486 Apple Tree ——(树型DP)

Apple Tree POJ - 2486