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

  1. 2 1
  2. 0 11
  3. 1 2
  4. 3 2
  5. 0 1 2
  6. 1 2
  7. 1 3

Sample Output

  1. 11
  2. 2

Source

POJ Contest,Author:magicpig@ZSU
  1. /*
  2. 给你一颗苹果树,每个节点都有相应的苹果树,让你求从结点1开始走最多走k步,能吃到的最多苹果数
  3. */
  4. #include<stdio.h>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<string.h>
  8. #include<vector>
  9. #define N 220
  10. #define INF 0x3f3f3f3f
  11. using namespace std;
  12. struct node
  13. {
  14. int to;
  15. node (int TO){to=TO;};
  16. };
  17. int n,k;
  18. int dp[N][N][];//dp[u][k]表示以u为根结点,走到k步时,返不返回根节点的最多获取多少苹果
  19. int val[N];//盛放每个点的苹果数
  20. vector<node>edge[N];
  21. /*
  22. (1)不能用记忆化搜索因为可能不是最后一步取到的最大值
  23.  
  24. (2)不是一条路径走到底能吃多少苹果,这样遍历,因为虽然走过一个地方就把这个地方的苹果吃光,但是如果走完一条路径的时候还有余下的步数可以返回
  25. 再接着吃别的路径的
  26.  
  27. */
  28. void dfs(int u,int p)//这一步,上一部,还剩多少步;
  29. {
  30. for(int i=;i<edge[u].size();i++)
  31. {
  32. int v=edge[u][i].to;
  33. if(v==p) continue;
  34. dfs(v,u);
  35. for(int j=k;j>=;j--)
  36. {
  37. for(int k=;k<=j;k++)
  38. {
  39. dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
  40. //从u到v回到u,在v中遍历的时候不回来
  41. //不返回根节点的,顶点u只用j-k步,剩下的给v,因为由u到v要耗费一步,所以在v点的时候最多只能走k步
  42. dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
  43. //从u点到v点,然后v点回来
  44. //不返回根节点的,顶点u只用j-k步,剩下的给v,因为由u到v,再由v到u要耗费两步,所以在v点的时候最多只能走k步
  45. dp[u][j][]=max(dp[u][j][],dp[u][j-k][]+dp[v][k-][]);
  46. //从u点到v点,然后v点中遍历回到v,再回到u
  47. //返回根节点的,顶点只用j-k步,剩下的给v,因为由u到v,再由v到u要耗费两步,所以在v点的时候最多只能走k步
  48. }
  49. }
  50. }
  51. }
  52. int main()
  53. {
  54. //freopen("in.txt","r",stdin);
  55. while(scanf("%d%d",&n,&k)!=EOF)
  56. {
  57. memset(dp,,sizeof dp);
  58. for(int i=;i<=n;i++)
  59. {
  60. scanf("%d",&val[i]);
  61. for(int j=;j<=k;j++)
  62. dp[i][j][]=dp[i][j][]=val[i];//初始化
  63. edge[i].clear();
  64. //cout<<val[i]<<" ";
  65. }
  66. //cout<<endl;
  67. int a,b;
  68. for(int i=;i<n;i++)
  69. {
  70. scanf("%d%d",&a,&b);
  71. edge[a].push_back(b);
  72. edge[b].push_back(a);
  73. }
  74. dfs(,);
  75. printf("%d\n",max(dp[][k][],dp[][k][]));
  76. }
  77. return ;
  78. }

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. 蓝色巨人IBM

    1911年IBM的前身CRT建立,在中华民国时期就与中国有很多商业合作,中国中央银行,中国银行,黄埔造船厂,建国后直到中美建交,IBM与中国的关系越来越紧密,今晚看了一遍关于蓝色巨人的视频,收益匪浅. ...

  2. openEntityForm时候如何给关于(regardingobjectid)类型查找字段赋值?

    本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复264或者20170924可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  3. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  4. 洗礼灵魂,修炼python(1)--python简介

    首先,本人也是刚接触python短短几个月,没有老鸟的经验和技能,大佬勿喷,以下所有皆是本人对python的理解 python,是一种解释型(高级)的,面向对象的,带有动态语义的高级程序设计的开源语言 ...

  5. 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. ORACLE 本地冷迁移

    需求:把oracle数据库的数据文件,redo文件,控制文件迁移到本地的其它目录. 1.测试环境: 操作系统redhat 6.3,数据库oracle 11.2.0.1.0 [root@dbtest1 ...

  7. yeah,我的博客成功建立!

    以此来记录我个人的学习历程!~~

  8. Google Authenticator 如何集成(U盾的实现原理相同)

    Google Authenticator是一个类似U盾的二次验证工具,Google提供了它的开源客户端(https://github.com/google/google-authenticator)里 ...

  9. 学习如何看懂SQL Server执行计划(二)——函数计算篇

    二.函数计算部分 --------------------标量聚合--------------------/* 标量聚合-主要在聚合函数操作中产生 计算标量:根据行中的现有值计算出一个新值 流聚合:在 ...

  10. Java web JavaScript DOM 编程

     JavaScript DOM 编程 (1).DOM概述及分类 (2).DOM结构模型:XML DOM 和 HTML DOM 关系? (3).结点,结点树,结点属性与方法? 1.DOM是什么? d ...