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 ...
随机推荐
- CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂+bsgs+exgcd)
题面 传送门 前置芝士 \(BSGS\) 什么?你不会\(BSGS\)?百度啊 原根 对于素数\(p\)和自然数\(a\),如果满足\(a^x\equiv 1\pmod{p}\)的最小的\(x\)为\ ...
- [ActionScript 3.0] 动态改变影片剪辑的颜色
flash.geom.ColorTransform 可使用 ColorTransform 类调整显示对象的颜色值.可以将颜色调整或颜色转换应用于所有四种通道:红色.绿色.蓝色和 Alpha 透明度. ...
- 爬虫--python3如何安装scrapy?
直接使用pip3 install scrapy会报很多错误,所以试试以下步骤. (1) https://www.lfd.uci.edu/~gohlke/pythonlibs/ 在这个python第三方 ...
- P2046 [NOI2010]海拔 平面图转对偶图(最小割-》最短路)
$ \color{#0066ff}{ 题目描述 }$ YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作 一个正方形,每一个区域也可看作一个正方形. ...
- 移动web之一像素问题
一.为什么会有一像素问题 弄明白这个问题,首先要知道DPR了. DPR(device pixel ratio)pixel等于picture element.设备像素比,是默认缩放100%的情况下,即D ...
- Codeforces Round #556 题解
Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...
- docker 安装sentry
主页:https://sentry.io/welcome/ 环境安装 请先安装 Docker 1.10+ ,使用 CE 版本:安装文档,写的很清晰,不详述:因为国内网络环境问题,一般建议 docker ...
- [转] git merge 将多个commit合并为一条之--squash 选项
[FROM] https://blog.csdn.net/themagickeyjianan/article/details/80333645 1.一般的做法(直接git merge) Git相对于C ...
- pipenv 虚拟环境新玩法
首先,虚拟环境本质是一个文件,是为了适应不同的项目而存在.pipenv相当于virtualenv和pip的合体. pipenv主要有以下特性: (1)pipenv集成了pip,virtualenv两者 ...
- dsad
dasdas dasdas dasdas dasdas