题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011

Starship Troopers

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

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
 
第二道树型dp,之前想用搜索做。最后看了题解。思路还是能够理解。
注意:树是无向的(双向)。当troop为0时不能再得到head(坑)。
树型dp还需继续理解,继续做题。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std; int n,m;
int cost[],value[];
int dp[][];
int father[];
vector<int> v[];
bool vis[]; void tree_dp(int root)
{
int fcost=cost[root];
vis[root]=;
for(int i=fcost;i<=m;i++)
dp[root][i]=value[root];
for(int i=;i<v[root].size();i++)
{
int son=v[root][i];
if(vis[son])
continue;
tree_dp(son);
for(int j=m;j>=fcost;j--)
{
for(int k=;k<=j-fcost;k++)
dp[root][j]=max(dp[root][j],dp[root][j-k]+dp[son][k]);
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(m!=-&&n!=-))
{
memset(vis,,sizeof(vis));
//memset(father,0,sizeof(father));
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
v[i].clear();
for(int i=; i<=n; i++)
{
int co;
scanf("%d%d",&co,&value[i]);
cost[i]=co/;
if(co%>)
cost[i]++;
//cout<<cost[i]<<'*'<<endl;
}
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
//father[b]++;
}
if(m==) {printf("0\n");continue;}
tree_dp();
printf("%d\n",dp[][m]);
}
return ;
}
 

HDU_1011_Starship Troopers_树型dp的更多相关文章

  1. POJ3659 Cell Phone Network(树上最小支配集:树型DP)

    题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...

  2. POJ 3342 - Party at Hali-Bula 树型DP+最优解唯一性判断

    好久没写树型dp了...以前都是先找到叶子节点.用队列维护来做的...这次学着vector动态数组+DFS回朔的方法..感觉思路更加的清晰... 关于题目的第一问...能邀请到的最多人数..so ea ...

  3. 【XSY1905】【XSY2761】新访问计划 二分 树型DP

    题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...

  4. 洛谷P3354 Riv河流 [IOI2005] 树型dp

    正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...

  5. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  6. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...

  7. ZOJ 3949 (17th 浙大校赛 B题,树型DP)

    题目链接  The 17th Zhejiang University Programming Contest Problem B 题意  给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...

  8. BZOJ 1564 :[NOI2009]二叉查找树(树型DP)

    二叉查找树 [题目描述] 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子结点的数据值小. 另一方面,这棵查找树中每个结点都有一个权值,每个结 ...

  9. Codeforces 149D Coloring Brackets(树型DP)

    题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...

随机推荐

  1. jquery验证插件validate自定义扩展

    <script src="${pageContext.request.contextPath}/resources/js/jquery-1.12.0.min.js" type ...

  2. .a 文件解析

    首先先准备一个静态库.a文件,比如叫staticLibrary.a,放在桌面的test目录里. 分离arch 首先先file一下staticLibrary.a,看一下该文件包含几种arch. ~ cd ...

  3. Redis缓存数据库安全加固指导(二)

    背景 在众多开源缓存技术中,Redis无疑是目前功能最为强大,应用最多的缓存技术之一,参考2018年国外数据库技术权威网站DB-Engines关于key-value数据库流行度排名,Redis暂列第一 ...

  4. [办公自动化]如何让excel图表标签中显示最新值数据

    同事做了一张excel图表,希望最新的数据显示数据标签,其他都不显示.并且当单元格的数据新增加时,这个标签要能自动更新. 这里需要用到公式,获取到这个最新值.在b2输入公式=lookup(9e+307 ...

  5. 【Codevs 4672】辛苦的老园丁

    http://codevs.cn/problem/4672/ 那个一看这不是(最大独立集)的最大权值和,类似 反图→ 最大团  NP问题 搜索解决 改一下模板即可 参考最大独立集  Maximum C ...

  6. [loj6089]小Y的背包计数问题

    https://www.zybuluo.com/ysner/note/1285358 题面 小\(Y\)有一个大小为\(n\)的背包,并且小\(Y\)有\(n\)种物品. 对于第\(i\)种物品,共有 ...

  7. Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible

    Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible bundle exec rake ...

  8. C#直接删除指定目录下的所有文件及文件夹(保留目录)

    #region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary&g ...

  9. mysql status关键字 数据表设计中慎重使用

    mysql status关键字  数据表设计中慎重使用

  10. jQuery插件之jqzoom的使用和参数设置

    jqzoom是一款基于jQuery的图片方法插件. 使用方法:1.引入jQuery与jqzoom,jqzoom.css 2.准备两张一大一小大小相同的图片,小图片放在<img>标签的&qu ...