Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17560    Accepted Submission(s): 4659

Problem Description
You,
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
The
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.

 
Output
For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.
 
Sample Input
5 10
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
 
Sample Output
50
7
 
Author
XU, Chuan
 
Source
 
题意:
树上有m个房间,每个房间里有bug和价值,一个士兵可以消灭20个bug,只有消灭掉房间里的所有bug才能得到价值,不能回到走过的房间现在有k个士兵问最多可以得到多少价值。
代码:

 /*
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的更多相关文章

  1. hdu1011 Starship Troopers 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 思路:很明显的树形背包 定义dp[root][m]表示以root为根,派m个士兵的最优解,那么d ...

  2. HDU-1011 Starship Troopers(树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  3. 树形dp专辑

    hdu 2196 http://acm.hdu.edu.cn/showproblem.php?pid=2196 input 5//5个结点 1 1//表示结点2到结点1有一条权值为1的边 2 1//表 ...

  4. 树形DP小结

    树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...

  5. 树形 DP 总结

    树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...

  6. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  7. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  8. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. 修改vs helpview手册路径

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Help\v2.1\Ca ...

  2. 删除表数据drop、truncate和delete的用法

    说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...

  3. 把textarea右下角的灰点去掉

    这个是浏览器自带功能,那个区域属于滚动条的占位区 使textarea可以调整size,加 style="resize:none"可解决,添加多行文字时还是会出现滚动条的.

  4. web_custom_request应用示例

    web_custom_request应用示例 LoadRunner提供的web_custom_request函数可以用于实现参数的动态生成.在LoadRunner中,web_reg_save_para ...

  5. [转载]C++声明和定义的区别

    <C++Primer>第四版 2.3.5节中这么说到: ①变量定义:用于为变量分配存储空间,还可为变量指定初始值.程序中,变量有且仅有一个定义. ②变量声明:用于向程序表明变量的类型和名字 ...

  6. JavaScript入门(2)

    encodeURI()和 decodeURI()作用  编码与解码 encodeURIComponent()和 decodeURIComponent()作用区别是  后者可以处理一些特殊字符进行转义 ...

  7. 数据库的SQL语句创建和主外键删除操作

    create table UserType ( Id ,), Name nvarchar() not null ) go create table UserInfo ( Id ,), LoginPwd ...

  8. BestCoder Round #68 (div.2)

    并查集 1002 tree 题意:中文题面 分析:(官方题解)把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=siz ...

  9. spring mvc 通过配置xml访问控制器的三种方式

    (一)通过 name 来一一映射(默认) (二)通过简单url 来指定映射,key 表示访问url value 是bean的ID (三)通过控制类的类名控制器,访问时类名首字母需要小写 <!-- ...

  10. JQuery EasyUI window 用法

    var $win; $win = $('#test-window').window({ title: '添加课程设置信息', width: 820, height: 450, top: ($(wind ...