好抽象的树形DP。。。。。。。。。

Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6411 Accepted: 2097

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 3Sample Output

11
2

Source

POJ Contest,Author:magicpig@ZSU

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

vector<int> g[200];
int N,K,valu[200],dp[2][200][300];
bool vis[200];

void Tree_Dp(int s)
{
    vis[s]=true;
    for(int i=0;i<=K;i++)
        dp[0][s]=dp[1][s]=valu[s];
    for(int i=0;i<g[s].size();i++)
    {
        int t=g[s];
        if(vis[t]) continue;
        Tree_Dp(t);
        for(int j=K;j>=0;j--)
        {
            for(int k=0;k<=j;k++)
            {
                dp[0][s][j+2]=max(dp[0][s][j+2],dp[0][t][k]+dp[0][s][j-k]);
                dp[1][s][j+2]=max(dp[1][s][j+2],dp[0][t][k]+dp[1][s][j-k]);
                dp[1][s][j+1]=max(dp[1][s][j+1],dp[1][t][k]+dp[0][s][j-k]);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&N,&K)!=EOF)
    {
        for(int i=1;i<=N;i++)
        {
            scanf("%d",valu+i);
            g.clear();
        }
        for(int i=0;i<N-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g.push_back(a);
        }
        memset(vis,false,sizeof(vis));
        memset(dp,0,sizeof(dp));
        Tree_Dp(1);
        printf("%d\n",max(dp[1][1][K],dp[0][1][K]));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2486 Apple Tree的更多相关文章

  1. poj 2486 Apple Tree(树形DP 状态方程有点难想)

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9808   Accepted: 3260 Descri ...

  2. POJ 2486 Apple Tree(树形dp)

    http://poj.org/problem?id=2486 题意: 有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值.(每个点只能获得一次) 思路: 从1点开始,往下dfs,对于每个 ...

  3. POJ 2486 Apple Tree ( 树型DP )

    #include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...

  4. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

  5. poj 2486 Apple Tree (树形背包dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: poj-2486 题意 给一个n个节点的树,节点编号为1~n, 根节点为1, 每个节点有一个权值.    从 ...

  6. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

  7. POJ 2486 Apple Tree (树形DP,树形背包)

    题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在 ...

  8. POJ 2486 Apple Tree (树形dp 经典题)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const ...

  9. POJ 2486 Apple Tree ——(树型DP)

    题意是给出一棵树,每个点都有一个权值,从1开始,最多走k步,问能够经过的所有的点的权值和最大是多少(每个点的权值只能被累加一次). 考虑到一个点可以经过多次,设dp状态为dp[i][j][k],i表示 ...

随机推荐

  1. Zabbix邮件报警-->Email

    Version:3.0.1 邮件报警有两种media 1.Email zabbix发送报警邮件到指定smtp服务器(使用系统自带的sendmail,发送邮箱是zabbix服务器的本地邮箱账号) 再由s ...

  2. kickstart note

    KickStart是一种无人职守安装方式 PXE + kickstart + DHCP + TFTP 1.挂载光盘 mount /dev/sr0 /mnt/iso cp -rf /mnt/iso/*  ...

  3. Linux下pdf阅读器推荐

    由于需要在pdf文件上做标记,所以自带的文档查看器根本满足了需求,之前去网上查了查,Okular评价挺高,就安装了一个,确实能基本满足我的需求,但是 1.界面感觉还是不太友好,书签栏一直在那. 2.而 ...

  4. css优化

    >>.li设置了display:inline-block,会有空隙,可在父元素ul下设置font-size:0 >>.ie下不支持margin:0 auto; >> ...

  5. 如何写出优雅的css代码 ?

    如何写出优雅的css代码 ? 对于同样的项目或者是一个网页,尽管最终每个前端开发工程师都可以实现相同的效果,但是他们所写的代码一定是不同的.有的优雅,看起来清晰易懂,代码具有可拓展性,这样的代码有利于 ...

  6. winndows7、office2013 激活信息还原

    windows7激活信息还原 1.现在“计算机”,右键中的“管理”中的“服务”中禁止Software Protection 2.还原路径C:\Windows\ServiceProfiles\Netwo ...

  7. HashMap与ArrayList互相嵌套的代码实现

    HashMap嵌套ArrayList的代码实现 结果要求为 三国演义            吕布            周瑜笑傲江湖           令狐冲            林平之神雕侠侣  ...

  8. ecshop 如果缩略图为空,使用默认图片

    引用:$row['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']); lib_common.php /** * 重新获 ...

  9. Unity 播放 视频

    Unity3D 播放视频 http://www.cnblogs.com/fortomorrow/archive/2012/11/01/unity07.html Unity3D研究院之两种方式播放游戏视 ...

  10. MIT Scheme 使用 Edwin

    MIT Scheme 的基本使用:http://www.math.pku.edu.cn/teachers/qiuzy/progtech/scheme/mit_scheme.htm 安装过程 安装bre ...