hdu 1011 Starship Troopers 经典的树形DP ****
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8540 Accepted Submission(s): 2379
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.
The last test case is followed by two -1's.
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
next one, instead you have to leave some troopers at each room passed to fight
all the bugs inside.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,m;
int w[],val[];
struct node
{
int next[];
int num;
}f[];
int dp[][];
bool use[]; int Max(int x,int y)
{
return x>y? x:y;
} void dfs(int k)
{
int i,j,t,s;
use[k]=true;
int num=(w[k]+)/;
for(i=num;i<=m;i++) dp[k][i]=val[k]; for(i=;i<=f[k].num;i++)
{
t=f[k].next[i];
if(use[t]==true) continue;
dfs(t);
for(j=m;j>=num;j--)
{
for(s=;s<=j;s++)
{
if(dp[k][j-s]!=-)
dp[k][j]=Max(dp[k][j],dp[k][j-s]+dp[t][s]);
}
}
}
} int main()
{
int i,x,y;
while(scanf("%d%d",&n,&m)>)
{
if(n==-&&m==-)break;
memset(dp,-,sizeof(dp));
memset(use,false,sizeof(use)); for(i=;i<=;i++)
f[i].num=; for(i=;i<=n;i++)
scanf("%d%d",&w[i],&val[i]);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
f[x].num++;
f[x].next[f[x].num]=y; f[y].num++;
f[y].next[f[y].num]=x;
}
if(m==)
{
printf("0\n");
continue;
}
dp[][m]=;
/*
1 1
21 1 --> 0
*/
dfs();
printf("%d\n",dp[][m]); }
return ;
}
hdu 1011 Starship Troopers 经典的树形DP ****的更多相关文章
- 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 ...
- hdu 1011 Starship Troopers(树形DP入门)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011 Starship Troopers 树形背包dp
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011(Starship Troopers,树形dp)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1011 Starship Troopers 树形DP 有坑点
本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...
- HDU 1011 Starship Troopers星河战队(树形dp)
题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [HDU 1011] Starship Troopers
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011 Starship Troopers(树上背包)
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
随机推荐
- PLSQL基本操作手册
第1章 用PLSQL连接Oracle数据库 PLSQL只能用来连接Oracle数据库(不象PB还可以连接JDBC.ODBC),所以必须首先安装并配置Oracle客户端. §1.1 初次登录PLSQL ...
- uC/OS-II 函数之消息队列相关函数
上文主要介绍了信号量相关的函数,本文介绍消息队列相关的函数:OSQCreate()建立消息队列函数,OSQPend()任务等待消息函数,其他的消息函数. 消息队列介绍 消息队列是µC/OS-II中另一 ...
- P4093 [HEOI2016/TJOI2016]序列
题目链接 题意分析 我们假设每一个数都有一个变动范围\([L_i,R_i]\) 那么我们令\(dp[i]\)表示以\(i\)结尾的最长不下降子序列的长度 那么就是\(dp[i]=max\{dp[j]+ ...
- 题目1016:火星A+B(字符串拆分)
问题来源 http://ac.jobdu.com/problem.php?pid=1016 问题描述 每次输入两个数,不同数位之间用逗号隔开,其中,第n位的进制就是第n个素数,即个位数是2进制的,十位 ...
- CSS 加号选择器("+")
加号选择器("+"):就是指对找到的某类的元素除第一个元素以外的兄弟元素起作用,即第一个元素不起作用,后面的兄弟元素都会起作用 效果:给每一个li加一个border-left, ...
- 微信小程序 没有找到node_modules目录 ,小程序引入vant框架报错。
如果大家是按照官网的引入方法,是否报错如图 不着急,大家就试试我的方法吧!两步走完美搞定! 第一步: 在小程序顶部点击设置->项目设置,会弹出下面的窗口.大家把使用npm模块勾上对勾. 第二步: ...
- Rstudio常用快捷键 “原版+中文” 整理
- 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest
题目传送门 只打了三个小时. A. Broadcast Stations B. Connect3 补题:zz 题解:因为格子是4*4的,而且每次落子的位置最多是只有四个,再加上剪枝,情况不会很多,直接 ...
- Python用于http/https接口自动化
本接口自动化框架主要用到的类: 1. unittest:组织测试用例 2. requests:http/https请求 3. HTMLTestRunner:生成测试报告 4. Dingtalkchat ...
- 使用app-inspector查看元素,无法连接到手机,提示错误{ Error: Command failed ……forward tcp:9001 tcp:9001错误解决
在学习使用app-inspector查看元素时,碰到一个问题.在cmd窗口执行命令app-inspector --port 5678 -u 85EABNFSU53R --verbose ,连接不到手 ...