题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数。

思路:

  既然是树,而且有限制k步,那么树形DP正好。

  考虑1个点的情况:(1)可能在本子树结束第k步(2)可能经过了j步之后,又回到本节点(第k步不在本子树)

  第二种比较简单,背包一下,就是枚举给本节点的孩子t多少步,收集到最多苹果数。第一种的话要求第k步终止于本节点下的某个子树中,那么只能在1个孩子子树中,所以应该是【其他孩子全部得走回来】+【本孩子不要求走回来】   or   【其他某个孩子中不走回来】+【本节点走回来】。

  用两个DP数组区分下“回”与“不回”就行了,注意,“不回”只能有1个孩子不要求其走回来,“回”是全部回。而“不要求回来”收集到的苹果数必定大于等于“要求回来”。

 //#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#define pii pair<int,int>
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
using namespace std;
const int N=;
struct node
{
int from,to,next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*];
int edge_cnt, head[N], w[N];
void add_node(int from,int to)
{
edge[edge_cnt]=node(from, to, head[from]);
head[from]=edge_cnt++;
}
/*
dp[][][1] 记录每次都回到本节点的。
dp[][][0] 记录仅1次不回到本节点的。
*/
int dp[N][N][];
void DFS(int t,int far,int m)
{
node e;
for(int j=; j<=m; j++) dp[t][j][]=dp[t][j][]=w[t]; //既然能到这,至少带上本节点
if(m==) return;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if( e.to^far )
{
DFS(e.to, t, m-);
for(int j=m; j>; j-- )
{
for(int k=; k+<=j; k++ )
{
//所有分支都回。
dp[t][j][]=max( dp[t][j][], dp[t][j-k-][]+dp[e.to][k][] );
//本分支要回,但在其他分支不回。因为已经有1个不回了,所以更新在‘[0]’中。
dp[t][j][]=max( dp[t][j][], dp[t][j-k-][]+dp[e.to][k][] );
}
for(int k=; k+<=j; k++ )
{
//不回,但其他分支就必须全回。
dp[t][j][]=max( dp[t][j][], dp[t][j-k-][]+dp[e.to][k][] );
}
}
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int n, K, a, b;
while(~scanf("%d%d",&n,&K))
{
edge_cnt=;
memset(head, -, sizeof(head)); for(int i=; i<=n; i++) scanf("%d",&w[i]);
for(int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(, -, K);
cout<<dp[][K][]<<endl;
}
return ;
}

AC代码

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 状态方程有点难想)

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

  3. POJ 2486 Apple Tree

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

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

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

  5. POJ 2486 Apple Tree(树形dp)

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

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

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

  7. POJ 2486 Apple Tree ( 树型DP )

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

  8. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

  9. POJ 2486 Apple Tree ——(树型DP)

    题意是给出一棵树,每个点都有一个权值,从1开始,最多走k步,问能够经过的所有的点的权值和最大是多少(每个点的权值只能被累加一次). 考虑到一个点可以经过多次,设dp状态为dp[i][j][k],i表示 ...

随机推荐

  1. 使用 DDMenuController 类的方法(非常好用的抽屉类)

    关于使用 DDMenuController 类的方法笔记:参考 DDMenuController 是一款非常好用的抽屉类文件. #pragma mark - 界面配置左右导航条的按钮 //[self ...

  2. <正则吃饺子>:关于使用powerDesign连接oracle数据库,导出数据表结构(ER图吧)

    最近做的项目中,没有完整的数据库表结构图(ER图),自己就根据服务器上oracle数据库和powerdesign整理一份,但是存在两个问题:1.没有把数据库表的相关备注弄下来:2.数据库表中的主外键关 ...

  3. python常用框架及第三方库

    python常用框架及第三方库 一.Web框架 1.Django: 开源web开发框架,它鼓励快速开发,并遵循MVC设计,比较庞大,开发周期短.Django的文档最完善.市场占有率最高.招聘职位最多. ...

  4. 使用Spring Security控制会话

    1.概述 在本文中,我们将说明Spring Security如何允许我们控制HTTP会话.此控件的范围从会话超时到启用并发会话和其他高级安全配置. 2.会话何时创建? 我们可以准确控制会话何时创建以及 ...

  5. 525. Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  6. PJzhang: github与出口管制·说明条款

    猫宁!!! 参考链接:https://www.infoq.cn/article/KMl2EO*PSMxIkVREiYvC https://help.github.com/en/articles/git ...

  7. 如何对接payjs的个人微信扫码支付接口

    在众多个人支付接口的产品中,要寻找一个稳定可靠的产品是比较难的,所幸遇到payjs,感觉逼格较高,非常满足自己的品味.推荐大家使用.下边是我在对接payjs的过程中的一些经验和技巧,分享给大家. 一. ...

  8. 最长上升序列(Lis)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  9. D - Bomb

    //反向62 #include <iostream> #include <algorithm> #include <string> #include <cst ...

  10. PostgreSQL-2-用户权限管理

    1.创建与删除用户 CREATE ROLE rolename; 方法1,创建角色 CREATE USER username; 方法2,创建用户 CREATE USER指令创建的用户默认是有登录权限的, ...