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
 
 #include"iostream"
#include"vector"
#include"cstring"
using namespace std;
const int size=;
int r,t;
int cost[size],brain[size];
int dp[size][size];
vector<int> adj[size];
void dfs(int p,int pre);
int main()
{
while(cin>>r>>t)
{
if(r==-&&t==-)
break;
int bug,a,b,i;
for(i=;i<r;i++)
{
cin>>bug>>brain[i];
cost[i]=(bug+)/;
}
for(i=;i<r;i++)
{
adj[i].clear();
}
for(i=;i<r-;i++)
{
cin>>a>>b;
adj[a-].push_back(b-);
adj[b-].push_back(a-);
}
if(t==)
{
cout<<''<<endl;
continue;
}
memset(dp,,sizeof(dp));
dfs(,-);
cout<<dp[][t]<<endl;
}
return ;
}
void dfs(int p,int pre)
{
for(int i=cost[p];i<=t;i++)
{
dp[p][i]=brain[p];
}
int num=adj[p].size();
for(int i=;i<num;i++)
{
int v=adj[p][i];
if(v==pre)
continue;
dfs(v,p);
for(int j=t;j>=cost[p];j--)
for(int k=;k<=j-cost[p];k++)
{
if(dp[p][j]<dp[p][j-k]+dp[v][k])
{
dp[p][j]=dp[p][j-k]+dp[v][k];
}
}
}
}

Starship Troopers的更多相关文章

  1. HD 1011 Starship Troopers(树上的背包)

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

  2. [HDU 1011] Starship Troopers

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

  3. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  4. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

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

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

  6. hdu 1011 Starship Troopers 树形背包dp

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

  7. HDU-1011 Starship Troopers(树形dp)

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

  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/有依赖的01背包】

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

随机推荐

  1. JSON解析关联类型发生死循环 There is a cycle in the hierarchy!

    解决办法是忽略掉关联类型的数据,使用jsonConfig进行配置,代码如下: JsonConfig jsonConfig = new JsonConfig();  //建立配置文件 jsonConfi ...

  2. Junit3.8

    使用Junit的最佳实践:新建一个名为test的source folder,用于存放测试类源代码目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面 测试 ...

  3. Java NIO 缓冲区学习笔记

    Buffer其实就是是一个容器对象,它包含一些要写入或者刚读出的数据.在NIO中加入Buffer对象,体现了新库与原I/O的一个重要区别.在面向流的I/O中,您将数据直接写入或者将数据直接读到Stre ...

  4. Java多线程编程模式实战指南:Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  5. C#类、静态类、静态变量,初始化执行顺序

    执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数

  6. [转]sql server 数据库日期格式化函数

    转至:http://www.cnblogs.com/hantianwei/archive/2009/12/03/1616148.html 0   或   100   (*)     默认值   mon ...

  7. oracle 全文检索技术

    1.查看用户: select * from dba_users WHERE username='CTXSYS';select * from dba_users WHERE username='CTXS ...

  8. do{...}while(0)的作用

    不是为了循环的while. 1.用于宏定义,保证宏一定按照想要的方式执行. #define   foo(x)     start(x); end(x) if(flag) foo(x); 扩展以后的结果 ...

  9. 比较器comparable与comparator的使用

    在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...

  10. 对PostgreSQL中tablespace 与 database, table的理解

    开始: 当前的tablesapce信息 pgsql=# select * from pg_tablespace; spcname | spcowner | spclocation | spcacl | ...