Starship Troopers

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1011
64-bit integer IO format: %I64d      Java class name: Main

 
 
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

Source

 
解题:树形dp,dp[u][j]表示节点u安排j个人
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m,bugs[],p[],dp[][];
vector<int>g[];
void dfs(int u,int fa){
int i,j,k;
for(i = bugs[u]; i <= m; i++)
dp[u][i] = p[u];
for(i = ; i < g[u].size(); i++){
if(g[u][i] == fa) continue;
dfs(g[u][i],u);
for(j = m; j >= bugs[u]; j--){
for(k = ; k <= j-bugs[u]; k++){
dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[g[u][i]][k]);
}
}
}
}
int main(){
int i,j,u,v;
while(scanf("%d %d",&n,&m),n != - && m != -){
for(i = ; i <= n; i++)
g[i].clear();
for(i = ; i <= n; i++){
scanf("%d %d",bugs+i,p+i);
bugs[i] = (bugs[i] + )/;
}
memset(dp,,sizeof(dp));
for(i = ; i < n; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
if(m == ) {puts("");continue;}
dfs(,-);
cout<<dp[][m]<<endl;
}
return ;
}
 

BNUOJ 5235 Starship Troopers的更多相关文章

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

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

  2. Starship Troopers

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

  3. [HDU 1011] Starship Troopers

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

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

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

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

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

  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/Other ...

  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. Lightoj 1071 - Baker Vai (双线程DP)

    题目连接: http://lightoj.com/volume_showproblem.php?problem=1071 题目大意: 一个n*m的格子,Baker Vai要从(1,1)到(n,m)再回 ...

  2. 【先定一个小目标】Postgresql允许远程访问配置修改

    1.解决不能连接远程postgresql: postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的l ...

  3. JS 事件添加onclick写法注意。

    自定义函数添加onclick事件写法注意. 错误写法:element.onclick = addclass(className); 正确写法:element.onclick = function(){ ...

  4. AJPFX总结正则表达式的概述和简单使用

    正则表达式的概述和简单使用* A:正则表达式        * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用.        * 作用:比如注 ...

  5. Hadoop YARN学习监控JVM和实时监控Ganglia、Ambari(5)

    Hadoop YARN学习监控JVM和实时监控Ganglia.Ambari(5) 1.0 监控ResourceManager进程Java虚拟机中堆空间的特定部分. jstat工具,在JDK的bin目录 ...

  6. kubernetesV1.13.1一键部署脚本(k8s自动部署脚本)

    部署k8sv1.13.1只需要下面几步就OK了: git clone https://github.com/luckman666/deploy_Kubernetes-v1.13.1.git cd de ...

  7. 新浪qq登录

    一:到腾讯QQ互联上申请APPID和APPKEY.申请地址: http://connect.qq.com/ 如同,这里我们可以获取到需要跳转到的APPID和APPKEY.新浪微博的申请同理 二:在Th ...

  8. 【译】x86程序员手册33-9.6中断任务和中断处理程序

    9.6 Interrupt Tasks and Interrupt Procedures 中断任务和中断处理程序 Just as a CALL instruction can call either ...

  9. SpringMVC 控制器统一异常处理

    摘要介绍spring mvc控制器中统一处理异常的两种方式:HandlerExceptionResolver以及@ExceptionHandler:以及使用@ControllerAdvice将@Exc ...

  10. cpio - 存取归档包中的文件

    总览 (SYNOPSIS) cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]a ...