Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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

思路:蜜汁AC,bug为0也需要去人;

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<cstring>
  10. #include<vector>
  11. #include<list>
  12. #include<bitset>
  13. #include<set>
  14. #include<map>
  15. #include<time.h>
  16. using namespace std;
  17. #define LL long long
  18. #define pb push_back
  19. #define mkp make_pair
  20. #define pi (4*atan(1.0))
  21. #define eps 1e-8
  22. #define bug(x) cout<<"bug"<<x<<endl;
  23. const int N=1e2+,M=2e6+,inf=1e9+;
  24. const LL INF=1e18+,mod=,MOD=;
  25.  
  26. int dp[N][N],n,m;
  27. int V[N],W[N];
  28. vector<int>edge[N];
  29. void dfs(int u,int fa,int m)
  30. {
  31. for(int i=V[u];i<=m;i++)
  32. dp[u][i]=W[u];
  33. for(int i=;i<edge[u].size();i++)
  34. {
  35. int v=edge[u][i];
  36. if(v==fa)continue;
  37. dfs(v,u,m-V[u]);
  38. for(int j=m;j>=V[u];j--)
  39. {
  40. for(int k=;j-k>=V[u];k++)
  41. if(dp[v][k])dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
  42. }
  43. }
  44.  
  45. }
  46.  
  47. int main()
  48. {
  49. while(~scanf("%d%d",&n,&m))
  50. {
  51. if(n==-&&m==-)break;
  52. memset(dp,,sizeof(dp));
  53. for(int i=;i<=n;i++)
  54. edge[i].clear();
  55. for(int i=;i<=n;i++)
  56. scanf("%d%d",&V[i],&W[i]),V[i]=(V[i]/)+(V[i]%?:);
  57. for(int i=;i<n;i++)
  58. {
  59. int u,v;
  60. scanf("%d%d",&u,&v);
  61. edge[u].pb(v);
  62. edge[v].pb(u);
  63. }
  64. if(!m)
  65. {
  66. printf("0\n");
  67. continue;
  68. }
  69. dfs(,,m);
  70. /*for(int i=1;i<=n;i++)
  71. {
  72. for(int j=0;j<=m;j++)
  73. cout<<dp[i][j]<<" ";
  74. cout<<endl;
  75. }*/
  76. printf("%d\n",dp[][m]);
  77. }
  78. return ;
  79. }

hdu 1011 Starship Troopers 树形背包dp的更多相关文章

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

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

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

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

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

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

  4. hdu 1011 Starship Troopers(树上背包)

    Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...

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

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

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

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

  7. HDU 1011 Starship Troopers【树形DP/有依赖的01背包】

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...

  8. hdu 1011(Starship Troopers,树形dp)

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

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

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

随机推荐

  1. いっしょ / Be Together (暴力枚举)

    题目链接:http://abc043.contest.atcoder.jp/tasks/arc059_a Time limit : 2sec / Memory limit : 256MB Score ...

  2. python requests库的简单使用

    requests是python的一个HTTP客户端库,跟urllib,urllib2类似,但比urllib,urllib2更加使用简单. 1. requests库的安装在你的终端中运行pip安装命令即 ...

  3. Python 监控脚本(硬盘、cpu、内存、网卡、进程)

    #磁盘使用率disk = psutil.disk_partitions()for i in disk:    print "磁盘:%s   分区格式:%s"%(i.device,i ...

  4. MySQL日志详细说明

    这片博文我们会详细说明MySQL本身的日志,不包含重做日志和undo日志(这两个日志是innodb存储引擎的日志). MySQL本身的日志有以下几种(MySQL5.7版本): 错误日志 慢查询日志 通 ...

  5. Golang接口简单了解

    在Golang中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口. package main import "fmt" type Animal interface ...

  6. linux时间修改-hwclock和date

    修改系统时间date 设定日期:date -s 月/日/年,例如设定日期为2018年12月1日,date -s 12/01/2018(年也可以是两位) 设定时间:date -s hh:mm:ss,例如 ...

  7. 常用MarkDown标记

    1:加粗 两个*号 加粗 2:代码段 三个` 代码段

  8. dell win 10笔记本关闭多媒体键,启用功能键的快捷方式

    自从使用win 10之后,在使用快捷键方面就没有win 7之前来的顺手,比如F8切换投影仪,F5/F6调试等等.特地搜了下,使用Fn+Esc可以在功能键和多媒体键之间切换.

  9. 牛客网数据库SQL实战(21-25)

    21.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序CREATE TABLE `employees` (`emp_no` i ...

  10. django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...