HDU1011 树形DP
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17560 Accepted Submission(s): 4659
the leader of Starship Troopers, are sent to destroy a base of the
bugs. The base is built underground. It is actually a huge cavern, which
consists of many rooms connected with tunnels. Each room is occupied by
some bugs, and their brains hide in some of the rooms. Scientists have
just developed a new weapon and want to experiment it on some brains.
Your task is to destroy the whole base, and capture as many brains as
possible.
To kill all the bugs is always easier than to capture
their brains. A map is drawn for you, with all the rooms marked by the
amount of bugs inside, and the possibility of containing a brain. The
cavern's structure is like a tree in such a way that there is one unique
path leading to each room from the entrance. To finish the battle as
soon as possible, you do not want to wait for the troopers to clear a
room before advancing to the next one, instead you have to leave some
troopers at each room passed to fight all the bugs inside. The troopers
never re-enter a room where they have visited before.
A starship
trooper can fight against 20 bugs. Since you do not have enough
troopers, you can only take some of the rooms and let the nerve gas do
the rest of the job. At the mean time, you should maximize the
possibility of capturing a brain. To simplify the problem, just maximize
the sum of all the possibilities of containing brains for the taken
rooms. Making such a plan is a difficult job. You need the help of a
computer.
input contains several test cases. The first line of each test case
contains two integers N (0 < N <= 100) and M (0 <= M <=
100), which are the number of rooms in the cavern and the number of
starship troopers you have, respectively. The following N lines give the
description of the rooms. Each line contains two non-negative integers
-- the amount of bugs inside and the possibility of containing a brain,
respectively. The next N - 1 lines give the description of tunnels. Each
tunnel is described by two integers, which are the indices of the two
rooms it connects. Rooms are numbered from 1 and room 1 is the entrance
to the cavern.
The last test case is followed by two -1's.
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
7
/*
DP方程不好理解。不足20个bug也要安排士兵。
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n,m,lne;
int dp[][];
int head[];
int a[],b[];
bool vis[];
struct node
{
int to,next;
}tree[];
void add(int u,int v)
{
tree[lne].to=v;
tree[lne].next=head[u];
head[u]=lne++;
}
void dfs(int root)
{
vis[root]=;
for(int i=a[root];i<=m;i++)
dp[root][i]=b[root];//能获得b的情况下,赋值
for(int i=head[root];i!=-;i=tree[i].next)
{
int son=tree[i].to;
if(vis[son])
continue;
dfs(son);
for(int j=m;j>=a[root];j--)
{
for(int k=;k+j<=m;k++)
dp[root][j+k]=max(dp[root][j+k],dp[root][j]+dp[son][k]);//一共带有j+k个士兵在root点放j个士兵
//dp[root][j+k]是在士兵数量够用的情
//况下的值,当这个父亲当儿子时他就有两个值一个是此值,一个是0,当dp[son][k]中的k大于等于他
//需要的士兵时dp[son][k]不是0,k小于他需要的士兵时dp[son][k]取0。
}
}
}
int main()
{
int u,v;
while(scanf("%d%d",&n,&m))
{
if(n==-&&m==-)
break;
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
a[i]=(a[i]+)/;
}
lne=;
for(int i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
if(m==) //没有士兵不能得到价值
{
printf("0\n");
continue;
}
dfs();
printf("%d\n",dp[][m]);
}
return ;
}
HDU1011 树形DP的更多相关文章
- hdu1011 Starship Troopers 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 思路:很明显的树形背包 定义dp[root][m]表示以root为根,派m个士兵的最优解,那么d ...
- HDU-1011 Starship Troopers(树形dp)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 树形dp专辑
hdu 2196 http://acm.hdu.edu.cn/showproblem.php?pid=2196 input 5//5个结点 1 1//表示结点2到结点1有一条权值为1的边 2 1//表 ...
- 树形DP小结
树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...
- 树形 DP 总结
树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
随机推荐
- loj 1271
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26981 思路:题目的意思是求给定的起点到终点的最短路径序列,并且这 ...
- Arduino101学习(一)——Windows下环境配置
一.Arduino IDE下载 要开发Arduino 101/Genuino 101,你需要先安装并配置好相应的开发环境.下载地址 http://www.arduino.cn/thread-5838- ...
- ExpandableListView 里面嵌套GridView实现高度自适应
很早之前做过一个商城的app 也是第一次做安卓. 实现的效果如下: 因为一开始做安卓,很多写的代码都不规范,在下面上代码的地方,还请高手指点(勿喷,楼主是自尊心很强的屌丝) 这个效果要解决2个大问题, ...
- sqoop与mysql之间中文乱码
sudo -u hive sqoop export --connect "jdbc:mysql://192.168.22.201/LauncherDB?useUnicode=true& ...
- Swift3.0语言教程使用占位符格式创建和初始化字符串
Swift3.0语言教程使用占位符格式创建和初始化字符串 Swift3.0语言教程使用占位符格式创建和初始化字符串在很多的编程语言中都存在占位符,占位符就是为指定的内容占留一个位置.此功能一般在开发者 ...
- [xsd学习]复合元素
对于xsd,复合元素的定义有两种方式: 一.在元素内部直接声明,此种方法只能此元素使用 <xs:element name="employee"> <xs:comp ...
- zookeeper + LevelDB + ActiveMQ实现消息队列高可用
通过集群实现消息队列高可用. 消息队列在项目中存储订单.邮件通知.数据分发等重要信息,故对消息队列稳定可用性有高要求. 现在通过zookeeper选取activemq leader的形式实现当某个ac ...
- 树状数组 + 位运算 LA 4013 A Sequence of Numbers
题目传送门 题意:n个数,两种操作,一是每个数字加x,二是查询& (1 << T) == 1 的个数 分析:因为累加是永远的,所以可以离线处理.树状数组点是c[16][M] 表示数 ...
- Android自动化测试 - MonkeyRunner(一)介绍
MonkeyRunner介绍: MonkeyRunner是Google提供的一个基于坐标点的Android黑盒自动化测试工具. Monkeyrunner工具提供了一套API让用户/测试人员来调用,调用 ...
- #asp.net core mvc 的视图注入
View injection is the most useful feature introduced in ASP.NET Core. 1.添加一个FruitsService public cla ...