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. JavaScript实战(带收放动画效果的导航菜单)

    虽然有很多插件可用,但为了共同提高,我做了一系列JavaScript实战系列的实例,分享给大家,前辈们若有好的建议,请务必指出,免得误人子弟啊! ( 原创文章,转摘请注明:苏福:http://www. ...

  2. JavaScript学习笔记6 之经典神坑题整理

    本篇内容是关于容易出错题的整理,这些题也有利于对javascript的语法逻辑的理解,分析的内容仅供参考: <script> */ var x=0 , y=0 , c=1; functio ...

  3. 谈谈JavaScript类型检测

      javascript内置的类型检测机制并非完全可靠.比如typeof操作符,并不能准确的判断数据是哪个类型,比如:数组和对象就不能通过typeof来区分. typeof [] ==="o ...

  4. ALV的颜色分为行的颜色、列的颜色和CELL的颜色

    ALV的颜色分为行的颜色.列的颜色和CELL的颜色.任务要求,将一定的Tabellenfeld 用黄色填充,也就是说CELL的颜色 DATA:ls_cellcolorTYPElvc_s_scol,co ...

  5. 高清VGA编码器|上海视涛科技

    VGA编码器(E200)简介 高清VGA编码器是上海视涛科技出品的高性能VGA编码产品.该VGA编码器是上海视涛科技完全自主研发,并适用于VGA信号的编码采集及网络传输的专用硬件设备.可兼容各厂家的N ...

  6. MyEclispe 2015 CI 15发布(附下载)

    MyEclipse 2015 CI 15带来了一些程序上的改进,包括可外部部署的JavaScript调 试,改进了 REST Inspect 和 WebSphere 框架支持,新增服务器连接器,另外还 ...

  7. Android Activity生命周期详讲

    管理 Activity 生命周期 通过实现回调方法管理 Activity 的生命周期对开发强大而又灵活的应用至关重要. Activity 的生命周期会直接受到 Activity 与其他 Activit ...

  8. Android中应用程序清除data/data,清除cache,超详细

    清除data,清除cache,其实在Android原生Setting里面有这个功能的. 需求是把这个功能做到自己的App里面,并计算出cache和data的size. 所以参考了一下Setting的源 ...

  9. iOS 在xib或storyboard里为控件添加圆角、外框和外框颜色

    如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以 layer.borderWidth     设置外框宽度属性 layer.cornerRadius    设置圆角属性 ...

  10. Mac OS 解压缩软件:StuffIt Expander

    由于今天下载一份文档,是在rar压缩包里,mac os系统默认是不支持RAR格式文件的解压的.在app store里找了下,找到一个免费的解压软件,名字叫:StuffIt Expander. 该软件由 ...