题目链接: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. 七夕节 看到很多停止更新的blog 莫名有点淡淡的忧桑

    又是一年七夕.又是一年单身.看到很多停止更新的blog, 仿佛看到了一茬一茬的程序猿 进入it 圈 又离开it圈,就有莫名的忧桑

  2. Wordpress 建站(一)

    去年在美国的justhost上买了两个域名(shanyexuanyu.com  和 chenjinyu.net.shanyexuanyu.com是给一位马来西亚的佛教徒朋友做的站点. 她镜头下佛教的文 ...

  3. linux设备驱动归纳总结(八):2.match.probe.remove

    linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  4. 使用mpxj读取MSPrjoect

    import java.util.ArrayList; import java.util.Calendar; import java.util.Hashtable; import java.util. ...

  5. jeasyUI的treegrid批量删除多行

    看上去,javascript的变量类型,也可以分为值类型和引用类型.赋值操作中,值类型,各自独立,互不干涉:引用类型,指针而已,大家指向同一个对象. 为什么这样说呢? 我是从jeasyUI的treeg ...

  6. qq xxs

    qq xxs qq xxs 有很多举个简单例子 : QQ空间发段文章 带上 <script> 提交(document.cookie) 到 自己服务端获取 </script> 在 ...

  7. Delphi异常处理的基本原则和方法

    Delphi异常处理的基本原则和方法 一.异常的来源. 在Delphi的应用程序中,下列的情况都比较有可能产生异常.(1)文件处理(2)内存分配(3)Windows资源(4)运行时创建对象和窗体(5) ...

  8. Linux在终端启动程序关闭终端不退出的方法

    一般情况下关闭终端时,那么在这个终端中启动的后台程序也会终止,要使终端关闭后,后台程序保持执行,使用这个指令: nohup 命令 & 如:nohup ./studio.sh & 网上其 ...

  9. Android 录音和摄像头权限适配【转】

    本文转载自:http://blog.csdn.net/self_study/article/details/52965045 最近在研究权限适配的相关内容,整理以前的权限博客如下:  android ...

  10. exynos 4412 时钟配置

    /** ****************************************************************************** * @author    Maox ...