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]表示不 ...
随机推荐
- Portlet简述
一.Portlet是什么? Portlet是基于java的web组件,由portlet容器管理,并由容器处理请求,生产动态内容.Portals使用portlets作为可插拔用户接口组件,提供信息系统的 ...
- clear both
原文地址:http://www.codefans.net/articles/653.shtml 因CSS很多布局是需要浮动的,当属性设置float(浮动)时,其所在的物理位置已经脱离文档流了,为了使f ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- npm 模块常用命令
mocha mocha --compilers js:babel/register : 在babel模式下测试,默认查找test文件夹,注意此时全局不要安装babel; ./node_modules/ ...
- iOS10 UI教程子视图和父视图UI层次结构和Views继承
iOS10 UI教程子视图和父视图UI层次结构和Views继承 iOS10 UI教程子视图和父视图UI层次结构和Views继承,本节将讲解与UI层次结构和Views继承相关的内容,其中包括子视图和父视 ...
- CSS3-margin,padding,border
margin padding border: 1.当属性值为0的时候,不需要在后面添加单位 2.当同时出现top margin以及bottom magin的时候,浏览器应用较大的哪一个 3.不能在 ...
- http://www.roncoo.com/article/detail/124661
http://www.roncoo.com/article/detail/124661
- Codeforces 660C Hard Process(尺取法)
题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...
- ssh An internal error occured during "Add Deployment"
这个问题一般是由于导入别人做的项目,但是项目所用的jdk跟当前所用的jdk不一样. 以前遇到过这个问题没有解决,今天解决了. 右键项目名→Properties→Java Build Path→Libr ...
- Oracle存储过程中临时表的使用技巧
一.Oracle临时表知识 在Oracle中,临时表分为SESSION(会话级).TRANSACTION(事务级)两种,SESSION级的临时表数据在整个SESSION都存在,直到结束此次SESSIO ...