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]表示不 ...
随机推荐
- 【rqnoj378】 约会计划
题目描述 cc是个超级帅哥,口才又好,rp极高(这句话似乎降rp),又非常的幽默,所以很多mm都跟他关系不错.然而,最关键的是,cc能够很好的调解各各妹妹间的关系.mm之间的关系及其复杂,cc必须严格 ...
- barabasilab-networkScience学习笔记5- Barabási-Albert 模型
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- 使用nginx做负载均衡的session共享问题
查了一些资料,看了一些别人写的文档,总结如下,实现nginx session的共享PHP服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果session不 ...
- ce游戏内存修改器(Cheat Engine)
ce修改器(Cheat Engine)一款专门修改内存修改编辑的游戏工具它包括16进制编辑,反汇编程序,内存查找工具新版6.1 版的CE与6.0 最大的区别就是添加了修改器制作工具,比之前 5.6.1 ...
- loadrunner怎么将变量保存到参数中
用这个lr_save_string 函数 char *b = "很简单";lr_save_string(b,"b"); lr_output_message(&q ...
- 从客户端(CourseIssueContent="<P>财务审计师岗位认证招生简章<BR>...")中检测到有潜在危险的 Request.Form 值。
说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止.该值可能指示危及应用程序安全的尝试,如跨站点的脚本攻击.通过在 Page 指令或 配置节中设置 validateRequest ...
- 【JavaScript】变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级
参考资料http://caibaojian.com/toutiao/5446 1.所有变量声明(var)或者声明函数都会被提升到当前函数顶部 关于函数表达式,js会将代码拆分为两行代码分别执行.这里需 ...
- express-3 最佳实践
版本控制 版本控制有以下益处: 文档: 能够回溯项目的历史,回顾所做的决策及组件的开发顺序,可形成宝贵的文档.记录项目的历史是十分有价值的. 归属: 团队工作,分工清晰,节省沟通成本. 试验: 你可以 ...
- PHP 采集
<?php header("content-type:text/html;charset=gbk"); // 要采集的页面的地址 $url = "http://ww ...
- [TJOI2016&HEOI2016]
很有意思.是因为排序那道题才听闻今年tjoi2016的. 题是好题!先把它刷完再去把zhihu look through一遍. bzoj4552 以后看到什么做不出的题,看看能否写二分!!!!写二分! ...