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

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

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
const int mod = ;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn = 1e4+;
int t,n,m,x,y;
int dp[maxn][],p[maxn],b[maxn];
int cost[maxn];
struct node
{
int u,v,nxt;
}e[maxn*];
int head[maxn];
int tot=;
void init()
{
ms(head,-);
ms(dp,);
}
void add(int u,int v)
{
e[tot].v=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)
{
for(int i=cost[u];i<=m;i++)
dp[u][i]=p[u]; //小于cost的无法获得经验 - dp[i][1]=val;
for(int i=head[u]; i!=-; i=e[i].nxt) //相当于背包种类
{
int v=e[i].v;
if(v==fa) continue;
dfs(v,u);
for(int j=m; j>=cost[u]; j--) //祖先的人力范围
{
for(int k=; k<=j-cost[u]; k++) //子树的人力范围
{
dp[u][j]=max(dp[u][j],dp[u][j-k] + dp[v][k]);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==-&&m==-) break;
init();
for(int i=;i<=n;i++)
{
scanf("%d %d",&b[i],&p[i]);
cost[i]=(b[i]+)/; //花费人力
}
for(int i=;i<n;i++)
{
scanf("%d %d",&x, &y);
add(x,y);
add(y,x);
}
if(!m)
{
printf("0\n");continue;
}
dfs(,-);
printf("%d\n",dp[][m]);
}
}
/* 【题意】
给你n和m代表n个点,m个士兵,要到下一个房间必须攻破上一个房间,每个士兵最多消灭20个BUG,就算不足20个BUG也要安排一个士兵
以1点为源点,向相邻的点移动,每个点有一个代价(值/20)和价值。
求花费m的士兵得到的最大价值是多少。 就是:给定一棵树,从1号顶点进入树中,每次可以分配人到其他可达的顶点去,杀死所有的bugs可以获取brain值,求出m个人最多能获取多少brain值。
【类型】
树形DP 【分析】
一看到价值和代价同时出现,马上想到背包,而且是有限的物品(结点),那么是01背包。
看出这是一棵树,那么就是树形01背包,对于树形DP自有一套应对方法。
设dp[i][j]:以i为根的子树有j个士兵的时候最多获得的价值。
转移方程:
dp[i][j] = max(dp[i][j], dp[i][j-k] + dp[son(i)][k]); 【时间复杂度&&优化】 【trick】
分析的时候自顶向下,实现自底向上,这也是dfs的思想,树形dp一般都是在dfs过程中实现的 【数据】 */

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. #应用openxml读写excel代码

    这个例子比较简单,没有考虑格式之类的问题. using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadshe ...

  2. 【poj1830-开关问题】高斯消元求解异或方程组

    第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...

  3. 【Codeforces711E】ZS and The Birthday Paradox [数论]

    ZS and The Birthday Paradox Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample ...

  4. bzoj 1705: [Usaco2007 Nov]Telephone Wire 架设电话线——dp

    Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= ...

  5. 透彻理解Spring事务设计思想之手写实现(山东数漫江湖)

    前言 事务,是描述一组操作的抽象,比如对数据库的一组操作,要么全部成功,要么全部失败.事务具有4个特性:Atomicity(原子性),Consistency(一致性),Isolation(隔离性),D ...

  6. HDU 2899 三分

    我们对这个函数求二阶导数,发现他的二阶导数是恒大于0的,那么他的导数是单调的,且在某时刻为0,那么这时的x值就是极值处的x值,其实题目说了,有最小值,那么我们三分水过去就好了. 反思:精度不够,因为是 ...

  7. Python 用ctypes观察Python对象的内存结构 -- (转)

    !!!强烈推荐的好文章!!! 对象的两个基本属性 Python所有对象结构体中的头两个字段都是相同的: refcnt:对象的引用次数,若引用次数为0则表示此对象可以被垃圾回收了. typeid:指向描 ...

  8. Java 中的静态内部类

    静态内部类是 static 修饰的内部类,这种内部类的特点是: 1. 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问 2. 如果外部类的静态成员与内部类的成 ...

  9. Linux中source命令的用法

    source命令: source命令也称为“点命令”,也就是一个点符号(.).source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录.因为linux所有的操作都会变成文 ...

  10. centos7安装libvirt支持xen

    另外还有一个非常棒的用法 假如我要执行iostat这个命令来查看CPU与存储设备状态,可是执行却发现没有这个命令 于是执行yum install iostat,结果说找不到该软件,使用下面的办法可以解 ...