POJ 2486 Apple Tree(树形dp)
http://poj.org/problem?id=2486
题意:
有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值。(每个点只能获得一次)
思路:
从1点开始,往下dfs,对于每个结点,把时间分配给它的子节点,然后求一个最大值。
但是要注意的是,它有可能会走了之后然后又走回到父亲结点,这样的话,状态转移方程需要多考虑一下。
d【u】【j】【0/1】表示从u结点出发走 j 时所能获得的最大权值(0/1表示是否返回u结点)。
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, k; int val[maxn];
int vis[maxn]; vector<int> g[maxn]; int d[maxn][maxn][]; void dfs(int u)
{
vis[u]=;
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(vis[v]) continue;
dfs(v);
for(int j=k;j>=;j--)
{
for(int t=;t<=j;t++)
{
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &k))
{
for(int i=;i<=;i++) g[i].clear();
memset(d,,sizeof(d)); for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
for(int j=;j<=k;j++)
d[i][j][]=d[i][j][]=val[i];
} for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} memset(vis,,sizeof(vis));
dfs();
printf("%d\n",max(d[][k][],d[][k][]));
}
return ;
}
POJ 2486 Apple Tree(树形dp)的更多相关文章
- poj 2486 Apple Tree(树形DP 状态方程有点难想)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9808 Accepted: 3260 Descri ...
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
- POJ 2486 Apple Tree (树形dp 经典题)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const ...
- 【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 ...
- POJ 2486 Apple Tree (树形DP,树形背包)
题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在 ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- poj 2486 Apple Tree (树形背包dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: poj-2486 题意 给一个n个节点的树,节点编号为1~n, 根节点为1, 每个节点有一个权值. 从 ...
- POJ 2486 Apple Tree ( 树型DP )
#include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...
随机推荐
- sort排序和uniq统计命令
author:headsen chen date: 2018-08-13 11:08:09 1,sort:排序的功能,默认安装ASCII码来排序,-n 安装数值排,-r 按照倒序来排 [root@b ...
- 树链剖分-点的分治(点数为k且距离最长的点对)
hdu4871 Shortest-path tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K ( ...
- vue视频: 自定义指令 && 拖拽 && 自定义键盘信息
v-textv-forv-html 指令: 扩展html语法 自定义指令:1. 自定义属性指令: Vue.directive(指令名称,function(参数){ this.el -> 原生DO ...
- 【模块化开发】------requireJS的基本使用------【巷子】
前言 为了提高代码的复用度,开发人员会按照功能把大量的js代码分成若干文件,这样在多个页面就可以使用同一个文件了.,下面是某个网站的js引用情况 虽然代码的复用度提升了,但是缺点也体现了出来 缺点: ...
- 用到了yii2 hasMany() 方法,一对多关联
view页面代码:其中supply,item,price是一个AR类都是一个类,item和prices是一对多关系: [ 'label' => '参考', 'format' => 'htm ...
- Android中的Apk的加固(加壳)原理解析和实现(转)
一.前言 今天又到周末了,憋了好久又要出博客了,今天来介绍一下Android中的如何对Apk进行加固的原理.现阶段.我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk, ...
- dongle --NFC
A dongle is a small piece of hardware that attaches to a computer, TV, or other electronic device in ...
- Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
网易公开课,第5课 notes,http://cs229.stanford.edu/notes/cs229-notes2.pdf 学习算法有两种,一种是前面一直看到的,直接对p(y|x; θ)进行建模 ...
- tomcat设置编码格式utf8
利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效! ...
- Python开发【模块】:PyMySQL
PyMySQL 1.安装 [root@localhost ~]# pip install PyMySQL 2.初识 创建数据表结构: mysql> CREATE TABLE `users` ( ...