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.

InputThe 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. 
OutputFor each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms. 
Sample Input

  1. 5 10
  2. 50 10
  3. 40 10
  4. 40 20
  5. 65 30
  6. 70 30
  7. 1 2
  8. 1 3
  9. 2 4
  10. 2 5
  11. 1 1
  12. 20 7
  13. -1 -1

Sample Output

  1. 50
  2. 7

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define rep(i,x,n) for(int i=(x); i<=(n); i++)
  36. using namespace std;
  37. typedef long long LL;
  38. typedef unsigned long long ULL;
  39. typedef pair<int,int> P;
  40. const int INF = 0x3f3f3f3f;
  41. const LL LNF = 1e18;
  42. const int maxm = 1e6 + ;
  43. const double PI = acos(-1.0);
  44. const double eps = 1e-;
  45. const int dx[] = {-,,,,,,-,-};
  46. const int dy[] = {,,,-,,-,,-};
  47. int dir[][] = {{,},{,-},{-,},{,}};
  48. const int mon[] = {, , , , , , , , , , , , };
  49. const int monn[] = {, , , , , , , , , , , , };
  50. const int mod = ;
  51. #define inf 0x3f3f3f3f
  52. #define ll long long
  53. const int maxn = 1e4+;
  54. int t,n,m,x,y;
  55. int dp[maxn][],p[maxn],b[maxn];
  56. int cost[maxn];
  57. struct node
  58. {
  59. int u,v,nxt;
  60. }e[maxn*];
  61. int head[maxn];
  62. int tot=;
  63. void init()
  64. {
  65. ms(head,-);
  66. ms(dp,);
  67. }
  68. void add(int u,int v)
  69. {
  70. e[tot].v=v;
  71. e[tot].nxt=head[u];
  72. head[u]=tot++;
  73. }
  74. void dfs(int u,int fa)
  75. {
  76. for(int i=cost[u];i<=m;i++)
  77. dp[u][i]=p[u]; //小于cost的无法获得经验 - dp[i][1]=val;
  78. for(int i=head[u]; i!=-; i=e[i].nxt) //相当于背包种类
  79. {
  80. int v=e[i].v;
  81. if(v==fa) continue;
  82. dfs(v,u);
  83. for(int j=m; j>=cost[u]; j--) //祖先的人力范围
  84. {
  85. for(int k=; k<=j-cost[u]; k++) //子树的人力范围
  86. {
  87. dp[u][j]=max(dp[u][j],dp[u][j-k] + dp[v][k]);
  88. }
  89. }
  90. }
  91. }
  92. int main()
  93. {
  94. while(~scanf("%d%d",&n,&m))
  95. {
  96. if(n==-&&m==-) break;
  97. init();
  98. for(int i=;i<=n;i++)
  99. {
  100. scanf("%d %d",&b[i],&p[i]);
  101. cost[i]=(b[i]+)/; //花费人力
  102. }
  103. for(int i=;i<n;i++)
  104. {
  105. scanf("%d %d",&x, &y);
  106. add(x,y);
  107. add(y,x);
  108. }
  109. if(!m)
  110. {
  111. printf("0\n");continue;
  112. }
  113. dfs(,-);
  114. printf("%d\n",dp[][m]);
  115. }
  116. }
  117. /*
  118.  
  119. 【题意】
  120. 给你n和m代表n个点,m个士兵,要到下一个房间必须攻破上一个房间,每个士兵最多消灭20个BUG,就算不足20个BUG也要安排一个士兵
  121. 以1点为源点,向相邻的点移动,每个点有一个代价(值/20)和价值。
  122. 求花费m的士兵得到的最大价值是多少。
  123.  
  124. 就是:给定一棵树,从1号顶点进入树中,每次可以分配人到其他可达的顶点去,杀死所有的bugs可以获取brain值,求出m个人最多能获取多少brain值。
  125. 【类型】
  126. 树形DP
  127.  
  128. 【分析】
  129. 一看到价值和代价同时出现,马上想到背包,而且是有限的物品(结点),那么是01背包。
  130. 看出这是一棵树,那么就是树形01背包,对于树形DP自有一套应对方法。
  131. 设dp[i][j]:以i为根的子树有j个士兵的时候最多获得的价值。
  132. 转移方程:
  133. dp[i][j] = max(dp[i][j], dp[i][j-k] + dp[son(i)][k]);
  134.  
  135. 【时间复杂度&&优化】
  136.  
  137. 【trick】
  138. 分析的时候自顶向下,实现自底向上,这也是dfs的思想,树形dp一般都是在dfs过程中实现的
  139.  
  140. 【数据】
  141.  
  142. */

HDU 1011 Starship Troopers【树形DP/有依赖的01背包】的更多相关文章

  1. hdu 1011 Starship Troopers(树形DP入门)

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

  2. HDU 1011 Starship Troopers 树形DP 有坑点

    本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...

  3. [HDU 1011] Starship Troopers (树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 dp[u][i]为以u为根节点的,花了不超过i元钱能够得到的最大价值 因为题目里说要访问子节点必 ...

  4. hdu 1011 Starship Troopers 树形背包dp

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

  5. hdu 1011 Starship Troopers(树形背包)

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

  6. HDU 1011 Starship Troopers 树形+背包dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1011   题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...

  7. HDU 1011 Starship Troopers (树dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...

  8. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

  9. hdu 1011 Starship Troopers 经典的树形DP ****

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

随机推荐

  1. JS学习之函数的属性和方法

  2. HEOI 2012 旅行问题

    2746: [HEOI2012]旅行问题 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1009  Solved: 318[Submit][Statu ...

  3. 【BZOJ】1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    [题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i] ...

  4. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

  5. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  6. ioctl( ) 函数

    ioctl( )函数 本函数影响由fd参数引用的一个打开的文件. #include<unistd.h> int ioctl( int fd, int request, .../* void ...

  7. linux平台学x86汇编语言学习集合帖

    linux平台学x86汇编语言学习集合帖 linux平台学x86汇编(一):https://blog.csdn.net/shallnet/article/details/45543237 linux平 ...

  8. uboot makefile构建分析

    前言 几年前分析过uboot的构建及启动过程,做了笔记,但最终没有转为文章.这次又有机会开发嵌入式产品了(之前一年多都是在搞x86 linux),看了下uboot的构建过程,觉得有必要写下整个分析过程 ...

  9. Ubuntu终端里面显示路径名称太长,怎么设置变短【转】

    转自:http://blog.csdn.net/id19870510/article/details/8276914 $: sudo vi ~/.bashrc 这个文件记录了用户终端配置 找到 if ...

  10. C语言的小括号----其实是逗号运算符

    比如下面的代码: #include <stdio.h> void fun() { int a, b, c, d; a = (, b = ); c = (, ); d = (, ); pri ...