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

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
/*
给你一颗苹果树,每个节点都有相应的苹果树,让你求从结点1开始走最多走k步,能吃到的最多苹果数
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define N 220
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int to;
node (int TO){to=TO;};
};
int n,k;
int dp[N][N][];//dp[u][k]表示以u为根结点,走到k步时,返不返回根节点的最多获取多少苹果
int val[N];//盛放每个点的苹果数
vector<node>edge[N];
/*
(1)不能用记忆化搜索因为可能不是最后一步取到的最大值 (2)不是一条路径走到底能吃多少苹果,这样遍历,因为虽然走过一个地方就把这个地方的苹果吃光,但是如果走完一条路径的时候还有余下的步数可以返回
再接着吃别的路径的 */
void dfs(int u,int p)//这一步,上一部,还剩多少步;
{
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].to;
if(v==p) continue;
dfs(v,u);
for(int j=k;j>=;j--)
{
for(int k=;k<=j;k++)
{
dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
//从u到v回到u,在v中遍历的时候不回来
//不返回根节点的,顶点u只用j-k步,剩下的给v,因为由u到v要耗费一步,所以在v点的时候最多只能走k步
dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
//从u点到v点,然后v点回来
//不返回根节点的,顶点u只用j-k步,剩下的给v,因为由u到v,再由v到u要耗费两步,所以在v点的时候最多只能走k步
dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
//从u点到v点,然后v点中遍历回到v,再回到u
//返回根节点的,顶点只用j-k步,剩下的给v,因为由u到v,再由v到u要耗费两步,所以在v点的时候最多只能走k步
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
for(int j=;j<=k;j++)
dp[i][j][]=dp[i][j][]=val[i];//初始化
edge[i].clear();
//cout<<val[i]<<" ";
}
//cout<<endl;
int a,b;
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(,);
printf("%d\n",max(dp[][k][],dp[][k][]));
}
return ;
}

poj 2486 Apple Tree(树形DP 状态方程有点难想)的更多相关文章

  1. POJ 2486 Apple Tree(树形DP)

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

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

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

  3. 【POJ 2486】 Apple Tree (树形DP)

    Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...

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

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

  5. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  6. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  7. POJ 2486 Apple Tree(树形dp)

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

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

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

  9. POJ 2486 Apple Tree ( 树型DP )

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

随机推荐

  1. php字符的替换,截取,指定查找

    <?php/** * Created by 郭鹏. * User: msi * Date: 2017/9/27 * Time: 14:17 *///随机数生成器echo rand();echo ...

  2. 读Zepto源码之IOS3模块

    IOS3 模块是针对 IOS 的兼容模块,实现了两个常用方法的兼容,这两个方法分别是 trim 和 reduce . 读 Zepto 源码系列文章已经放到了github上,欢迎star: readin ...

  3. http://codeforces.com/problemset/problem/847/E

    E. Packmen time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. 【JAVA零基础入门系列】Day3 Java基本数据类型

    前两篇已经将开发环境搭建完成,如果你已经按之前的教程按部就班的完成了部署,那么世界上最优秀的编程语言之一和世界上最优秀的IDE之一已经出现在你的电脑上(此处应有掌声),如果你还没入门,或者正在台阶上踱 ...

  5. ch2-mysql相关

    1 mySql数据库基本 1.1 创建表必须字段 id 1.2 nodeJS数据库连接 根目录下建立 mysql.js 文件代码 var mysql = require('mysql'); var c ...

  6. 程序员 各种PDF格式电子书--免费网盘资源

    Java <设计模式之禅(完整高清版)> 链接:http://pan.baidu.com/s/1bo7noMb 密码:5kve  <重构_改善既有代码的设计> 链接:http: ...

  7. zoj 2022

    分析: 组合数学类型的题目. 正常的话可能会去分解1~N数里面有几个5和2,但是这样的复杂度为O(nlogn). 其实有更巧妙的办法,可以把问题分解成子问题. 可以发现N!末尾的0与1~N中有几个5的 ...

  8. java web 学习笔记 编码问题总结

       java web 学习笔记 编码问题总结 1.非form表单中提交的中文参数---------------------------传递给Servlet服务器时,默认以iso-8859-1解码 ...

  9. PHP开发要点与技巧总结(一)

    Opcache:Opcache 来源于Zend Optimizer+改名,主要作用是通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是省去了每次加载 ...

  10. HTTP错误代码大全

    HTTP出错大全 101 - Switching Protocols Top Success Codes 200 - OK201 - Created202 - Accepted203 - Non-Au ...