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
 
解:树上背包模板题,需要注意的是不足20的部分也需要1个人
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=+;
const int maxm=+; vector<ll>edges[maxn]; ll f[maxn][maxm]; ll n,m;
ll v[maxn],c[maxn]; void addedge(ll u,ll v){
edges[u].push_back(v);
edges[v].push_back(u); } void clear_(ll n){
for(ll i=;i<=n;i++)edges[i].clear();
memset(f,,sizeof(f));
} void dp(ll x,ll fa){
for(ll i=;i<(ll)edges[x].size();i++){
ll y=edges[x][i];
if(y!=fa){
dp(y,x);
for(ll t=m;t>=;t--){
for(ll j=t;j>=;j--){
if(t-j>=){
f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
}
}
}
}
}
for(ll t=m;t>;t--){
if(t>=c[x])f[x][t]=f[x][t-c[x]]+v[x];
else f[x][t]=;
}
} int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
if(n==m&&m==-)break;
for(int i=;i<=n;i++){
scanf("%lld%lld",&c[i],&v[i]);
c[i]=(c[i]+)/;
} clear_(n);
for(int i=;i<n;i++){
ll u,v;
scanf("%lld%lld",&u,&v);
addedge(u,v);
}
dp(,);
printf("%lld\n",f[][m]);
}
return ;
}

hdu 1011 Starship Troopers(树上背包)的更多相关文章

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

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

  2. hdu 1011 Starship Troopers 树形背包dp

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

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

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

  4. 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 ...

  5. HD 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 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...

  7. [HDU 1011] Starship Troopers

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

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

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

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

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

随机推荐

  1. 刚安装了ftp之后无法使用root访问,服务器发回了不可路由的地址。使用服务器地址代替。

    真的艰辛,用了整整一个下午加晚上,才把服务器搭建好,中间真的好多坑... 错误1: vsftpd正确配置: vsftpd.conf: pam_service_name=vsftpduserlist_e ...

  2. Go语言实现:【剑指offer】丑数

    该题目来源于牛客网<剑指offer>专题. 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7.习惯上我们把1当做是第一个丑 ...

  3. Elemetary OS deepin-wine 版 微信 和 企业微信 输入框字体显示异常解决

    Elemetary OS deepin-wine 版 微信 和 企业微信 输入框字体显示异常解决 最近体验了Elementary OS 这个系统, 话说我之前都是用的Manjora 的各个桌面版, 突 ...

  4. Vue项目使用vant框架

    近期在开发h5端项目,用到vant框架,vant是一款基于Vue的移动UI组件,看了vant的官方文档(https://youzan.github.io/vant/#/zh-CN/)感觉不错,功能比较 ...

  5. php 关于php创建 json文件 和 对文件增删改查 示例

    <?php //fopen($file,'w'); //打开文件/创建文件 //file_put_contents($file,$content)//写入文件 //file_get_conten ...

  6. golang函数 和 条件语句

    /* if : if 语句 由一个布尔表达式后紧跟一个或多个语句组成 is else : if 语句 后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行 s ...

  7. VFP9.0的GDI+类的使用

    GDI+你应该不会陌生吧,然而,在VFP里要使用这一技术,可不是一件容易的事,你得学习一大堆API函数.或许,一想到这,你已经望而却步了.不过,从现在起,这一技术不再是豪门旺族的专宠了,我们每一位Fo ...

  8. Shell脚本 硬盘监控

    用shell 写了一个硬盘监控的小程序   #!/bin/bash MAX=95 EMAIL=zonghua@iyunshu.com PART=sda1   IP=`ifconfig |grep -w ...

  9. 修改 ssh 远程连接 时间

           linux使用的是  红帽旗下 centos.         解释两个文件          /etc/ssh/sshd_config     配置ssh服务器端的          ...

  10. weblogic非正常关闭,<BEA-141281>错误

    在域下 find -name *.lok , 全删除: 在域下 find -name *.DAT,全删除: 然后就可以正常启动了