POJ - 2486 Apple Tree【树形DP】

Apple Tree

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12489   Accepted: 4264

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

POJ Contest,Author:magicpig@ZSU

因为可以走回头路,所以每个点分两种情况。

dp[i][j][2]

(1)dp[i][j][0]从i点出发走j步并且最后回到i点。

(2)dp[i][j][i]从i点出发走j步并且最后不回到i点。

若回到i点,那么dp[i][j][0]=max(dp[v][k-2][0]+dp[i][j-k][0]),其中v为i的子节点,dp[i][j-k][0]表示已经遍历过的子节点的max,枚举k值。

若不回到i点,那么

(1)dp[i][j][1]=max(dp[v][k-2][0]+dp[i][j-k][1])  表示走当前子节点并且返回,然后走其他节点

(2)dp[i][j][1]=max(dp[v][k-1][1]+dp[i][j-k][0])  表示走其他节点返回,然后走子节点

枚举k即可。

代码中表示变量略有变动

#include "cstdio"
#include "cstring"
#include "queue"
#include "iostream"
#include "vector"
using namespace std;
const int inf=0x3f3f3f3f;
int num[104];
int dp[104][204][2];
struct edge
{
    int v,nxt;
}g[204];
int head[104];
int vis[104][204];
int cnt;
int n,k;
void addedge(int u,int v)
{
    g[cnt].v=v;
    g[cnt].nxt=head[u];
    head[u]=cnt;
    ++cnt;
}
void dfs(int u,int pre)
{
    dp[u][0][0]=num[u];
    dp[u][0][1]=num[u];
    for (int i = head[u]; i != -1 ; i=g[i].nxt) {
        int v=g[i].v;
        if(v==pre)continue;
        dfs(v,u);
        for (int j = k; j >= 0; --j) {//此处必须倒序枚举,避免后效性,即可能出现重复计数
            for (int l = 1; l <= j; ++l) {
                if(l>=2)
                {
                    dp[u][j][0]=max(dp[u][j][0],dp[v][l-2][0]+dp[u][j-l][0]);//回到u点
                   dp[u][j][1]=max(dp[u][j][1],dp[v][l-2][0]+dp[u][j-l][1]);//不回
                }
                dp[u][j][1]=max(dp[u][j][1],dp[v][l-1][1]+dp[u][j-l][0]);//不回
            }
        }
    }
}
void init()
{
    memset(head,-1, sizeof(head));
    cnt=0;
    memset(dp,0, sizeof(dp));
}
int main()
{
    while(cin>>n>>k)
    {
        init();
        for (int i = 1; i <= n; ++i) {
            scanf("%d",&num[i]);
        }
        for (int i = 0; i < n-1; ++i) {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        dfs(1,-1);
        int ans=0;
        for (int i = 0; i <= k; ++i) {
            ans=max(ans,dp[1][i][1]);
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/86062155