Apple Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9989   Accepted: 3324

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步之内最大值

典型树形背包,每个点容量为k的背包每个字节点j是一个组每个组k个物品
问题在于,如果想到别的孩子去,还要回到根
d[i][j][0/1]表示子树i走j步回到根/不回到根的最大值
转移分三个:d[i][j][0]一种,d[i][j][1]两种:可以是当前孩子不回来,也可以是当前孩子回来
 
一些细节:
1.不一定走k步,先把所有容量都装上自己w[u]
2.这样的话不要用son了,容量都用k
//
// main.cpp
// poj2486
//
// Created by Candy on 9/27/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e2+,K=2e2+,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n=,k,u,v,w[N];
struct edge{
int v,ne;
}e[N<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][K][];
void dp(int u,int fa){
for(int i=;i<=k;i++)d[u][i][]=d[u][i][]=w[u];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dp(v,u);
for(int j=k;j>=;j--){
for(int z=;z<=j;z++){
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
}
//printf("d %d %d %d %d\n",u,j,d[u][j][0],d[u][j][1]);
}
}
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
cnt=;memset(h,,sizeof(h));
for(int i=;i<=n;i++) w[i]=read();
for(int i=;i<=n-;i++){u=read();v=read();ins(u,v);}
//memset(f,0,sizeof(f));
dp(,-);
printf("%d\n",d[][k][]); // cout<<"\n\n";
// for(int i=1;i<=n;i++ ) printf("son %d %d\n",i,son[i]);
}
}

poj2486Apple Tree[树形背包!!!]的更多相关文章

  1. Kattis - redblacktree Red Black Tree (树形背包)

    问题:有一课含有n(n<=2e5)个结点的数,有m(m<=1000)个结点是红色的,其余的结点是黑色的.现从树中选若干数量的结点,其中红色的恰有k个,并且每个结点都不是其他任何另一个结点的 ...

  2. 【bzoj4987】Tree 树形背包dp

    题目描述 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. 输入 第一行两个正整数n,k,表示数的顶点数和需要选出的点个数. 接下 ...

  3. ZOJ - 3201 Tree of Tree (树形背包)

    题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...

  4. POJ2486 Apple Tree(树形背包)

    从每个节点u出发后有两种情况:回到u和不回到u. dp数组设为三维,第一维是节点编号,第二维是从该节点开始走的步数,第三维1/0 表示是否回到该节点. 可以回到时:dp[u][j][1]=max(dp ...

  5. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  6. poj 1155 树形背包

    http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...

  7. UVa 1407 树形背包 Caves

    这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...

  8. HDU1561 The more ,The better (树形背包Dp)

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

  9. 使用ztree.js,受益一生,十分钟学会使用tree树形结构插件

    看到ztree.js,这几个字眼,毋庸置疑,那肯定就是tree树形结构了,曾经的swing年代有jtree,后来jquery年代有jstree和treeview,虽然我没写过,但是我见过,一些小功能做 ...

随机推荐

  1. Windows安装apache2.4

    The primary Windows platform for running Apache 2.4 is Windows 2000 or later. Always obtain and inst ...

  2. HTML5移动开发学习笔记

    最近做webapp项目过程中,发现坑还是挺多的,以下是网络收集一些开发中的常见问题及知识汇总,以便查阅,慢慢更新:). meta 基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 < ...

  3. Swift安装

    Server1 .Update sudo apt-get update sudo apt-get upgrade . sudo apt-get install bridge-utils .IP 3.1 ...

  4. POI中操作PPT获得每页的TABLE

    HSLFSlideShow slideShow = new HSLFSlideShow(bufferInputUtil.getBufferedInputStream()); logger.info(& ...

  5. Linux下运行windows程序

    现在Winxp停止了支持,那我们的windows程序是否可以再linux上执行呢,如下是一些参考的信息 在您的 Linux/Mac 操作系统上运行 Windows 软件 http://www.wine ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q6-Q8)

    Question 6  You are designing a SharePoint 2010 solution that allows users to enter address informat ...

  7. VC方法调用顺序

    VC1,push VC2  vc2 pop 到vc1  右滑一半,然后取消 vc1 present到vc2  vc2 dismiss到vc1  push和present是不同的.表现在两个vc ...

  8. Android+PHP服务器+MySQL实现安卓端的登录

    时隔已久的一个任务,今天终于可以画上一个句号了.心情是万分的激动,虽然这份小成就来的有点迟但还是按捺不住心情的澎湃.下面我就先上几张图片来展示一下我的成绩 android源代码: 首先最重要的一件事是 ...

  9. 优化MySchool数据库(一)

    <优化MyShcool数据库>:能够的独立的分析|设计|创建|运营|你的独立的数据库系统 设计--->实现--->TSQL--->查询优化---->性能优化技术-- ...

  10. Android Build Error(1)

    Type 1 —— Build Path Problem : **.jar包文件缺失 1.在Android项目根目录下新建一个libs文件夹: 2.把你需要的导入的第三方Jar包复制进这个目录: 3. ...