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. 06讲案例篇:系统的CPU使用率很高,但为啥却找不到高CPU的应用

    小结 碰到常规问题无法解释的 CPU 使用率情况时,首先要想到有可能是短时应用导致的问题,比如有可能是下面这两种情况. 第一,应用里直接调用了其他二进制程序,这些程序通常运行时间比较短,通过 top ...

  2. eclipse编写代码所遇到的问题

    spring方面: 1.Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListabl ...

  3. GORM CRUD指南

    CRUD通常指数据库的增删改查操作,本文详细介绍了如何使用GORM实现创建.查询.更新和删除操作. CRUD CRUD通常指数据库的增删改查操作,本文详细介绍了如何使用GORM实现创建.查询.更新和删 ...

  4. Codeforces 1249F Maximum Weight Subset (贪心)

    题意 在一颗有点权的树上,选若干个点,使得这些点两两距离大于k,且点权和最大 思路 贪心的取比较大的值即可 将所有点按照深度从大到小排序,如果当前点点权\(a[i]\)大于0,则将距离为k以内的所有点 ...

  5. 用pyinstaller打包时的图标问题

    前言 因为昨天重新研究了下python的打包方法,今天一番准备把之前写的一个pdf合并软件重新整理一下,打包出来. 但在打包的过程中仍然遇到了一些问题,半年前一番做打包的时候也遇到了一些问题,现在来看 ...

  6. 使用IDEA详解Spring中依赖注入的类型(上)

    使用IDEA详解Spring中依赖注入的类型(上) 在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件 ...

  7. 使用Java8 Files类读写文件

    Java8 Files类的newBufferedReader()和newBufferedWriter()方法 这两个方法接受Path类型的参数.Path 类是Java8 NIO中的接口.可以由Path ...

  8. qt creator源码全方面分析(2-10-1)

    目录 Getting and Building Qt Creator 获取Qt 获取和构建Qt Creator Getting and Building Qt Creator 待办事项:应该对此进行扩 ...

  9. Danganronpa 水题。

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. href的几个特殊值

    a href ="" :默认打开的还是当前页面,会刷新一下重新打开. a href ="#": 浏览器地址栏网址后面会多显示1个#.不会刷新页面,会回到页面顶部 ...