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. java大数取模

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 用java写大数果然是方便多了! import java.math.BigInt ...

  2. 简单用DOM4J结合XPATH解析XML

    由于DOM4J在解析XML时只能一层一层解析,所以当XML文件层数过多时使用会很不方便,结合XPATH就可以直接获取到某个元素 使用dom4j支持xpath的操作的几种主要形式    第一种形式   ...

  3. WPD:Page Download Time Breakdown选项详解

    WPD:Page Download Time Breakdown选项详解 “页面下载时间细分”图显示每个页面组件下载时间的细分,可以根据它确定在网页下载期间事务响应时间缓慢是由网络错误引起还是由服务器 ...

  4. 网页打印A4纸-----表格在跨页时自动换页打印的实现 (转)

    在最近所做的一个项目中,需要通过网页来打印不少的表单,但是又不想每个打印页签各占用一个页面,这样就需要生存很多不同的冗余页面,为了减少冗余,所有的表单通过jquery的页签tab来实现的. 一 :基本 ...

  5. winform里怎样在一个按钮上实现“单击”和“双击”事件?

    Button按钮是没有双击事件(DoubleClick)的. button1.DoubleClick+=new EventHandler(button1_DoubleClick);使用这种方法在双击的 ...

  6. HTML5实践 -- 使用CSS3 Media Queries实现响应式设计

    CSS3 Media用法介绍:http://www.w3cplus.com/content/css3-media-queries 转载请注明原创地址:http://www.cnblogs.com/so ...

  7. POJ2976 Dropping tests(01分数规划)

    题目大概说给n个二元组Ai和Bi,要去掉k个,求余下的100*∑Ai/∑Bi的最大值. 假设要的最大的值是ans,令Di=Ai-ans*∑Bi,对Di排序取最大的n-k个,如果∑Ai-ans*∑Bi& ...

  8. ural 1147. Shaping Regions

    1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...

  9. The 2015 China Collegiate Programming Contest L. Huatuo's Medicine hdu 5551

    Huatuo's Medicine Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  10. So you want to be a 2n-aire?[HDU1145]

    So you want to be a 2n-aire?Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...