题目链接: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. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  2. zipfile zip

    Python模块学习:zipfile zip文件操作 2015/05/27 · 系列教程 · zipfile 分享到:3 原文出处: DarkBull    最近在写一个网络客户端下载程序,用于下载服 ...

  3. CF:Problem 427C - Checkposts强连通 Tarjan算法

    tarjan算法第一题 喷我一脸. ...把手写栈的类型开成了BOOL.一直在找错.. . #include<cstdio> #include<cstring> #includ ...

  4. Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                                   D. Painting The Wall ...

  5. canvas绘图数学知识总结

    题外话: 最近看了一本书叫 <HTML5 Canvas核心技术 图形.动画与游戏开发>已经算是看了85%,基本接近尾声,所以近期会多总结一些关于canvas的东西, 这本书讲的还算可以,最 ...

  6. 关于Time Series Database

    今天观察了下clojure之父datomic.com的数据库 有点类似sequence数据库的变种 不过这类数据库,感觉用在移动端会更有市场: 1. 移动端需要这种可以本地存储,无删除,联网只增加,只 ...

  7. YTU 2535: C++复数运算符重载(+与<<)

    2535: C++复数运算符重载(+与<<) 时间限制: 1 Sec  内存限制: 128 MB 提交: 867  解决: 532 题目描述 定义一个复数类Complex,重载运算符&qu ...

  8. SGU 145

    节点不可重复经过的K短路问题. 思路:二分路径长度,深搜小于等于路径长度的路径数.可以利用可重复点K短路问题中的A*函数进行剪枝. 尝试另一种解法:把可重复点K短路A*直接搬过来,堆中的每个元素额外记 ...

  9. python-----使用requirements.txt批量安装包

    首先写一个 requirements.txt,格式如图: 然后使用命令行,到 requirements.txt 所在的目录下,执行命令: pip install -r requirements.txt ...

  10. 【154】C#打包程序成安装包

    参考0:用C#写完程序怎么用C#打包成安装程序setup自己做的图文说明示例 参考1:解决“默认公司名称” C#打包应用安装后,显示“默认公司名称”,想问问通过哪里可以修改??? 参考2:解决“添加卸 ...