hdu1011 Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14426 Accepted Submission(s): 3887
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.
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.
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
7
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 105
int first[maxn],vis[maxn],w[maxn],value[maxn];
int dp[maxn][maxn];
struct node{
int to,next;
}e[2*maxn];
int n,m;
void dfs(int u)
{
int i,j,v,t,k;
vis[u]=1;
int flag=0;
if(w[u]%20==0)t=w[u]/20;
else t=w[u]/20+1;
for(i=t;i<=m;i++)dp[u][i]=value[u]; //这里要先初始化为value[u]
for(i=first[u];i!=-1;i=e[i].next){
v=e[i].to;
if(vis[v])continue;
flag=1;
dfs(v);
for(j=m;j>=t;j--){
for(k=j-t;k>0;k--){ //这里k不能等于0,因为如果不派队伍,那么得到的概率一定是0
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
}
}
}
for(j=t-1;j>=0;j--){
dp[u][j]=0;
}
if(flag==0){
if(w[u]%20==0)t=w[u]/20;
else t=w[u]/20+1;
for(j=t;j<=m;j++)dp[u][j]=value[u];
}
}
int main()
{
int i,j,c,d,tot;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==-1 && m==-1)break;
for(i=1;i<=n;i++){
scanf("%d%d",&w[i],&value[i]);
}
memset(first,-1,sizeof(first));
tot=0;
for(i=1;i<=n-1;i++){
scanf("%d%d",&c,&d);
tot++;
e[tot].next=first[c];e[tot].to=d;
first[c]=tot;
tot++;
e[tot].next=first[d];e[tot].to=c;
first[d]=tot;
}
if(m==0){
printf("0\n");continue;
}
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
dfs(1);
printf("%d\n",dp[1][m]);
}
return 0;
}
hdu1011 Starship Troopers的更多相关文章
- 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+分组背包)
题目大意:给一棵有根带点权树,并且给出容量.求在不超过容量下的最大权值.前提是选完父节点才能选子节点. 题目分析:树上的分组背包. ps:特判m为0时的情况. 代码如下: # include<i ...
- hdu1011 Starship Troopers 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 思路:很明显的树形背包 定义dp[root][m]表示以root为根,派m个士兵的最优解,那么d ...
- HD 1011 Starship Troopers(树上的背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Starship Troopers
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
- [HDU 1011] Starship Troopers
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- 杭电OJ——1011 Starship Troopers(dfs + 树形dp)
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...
- hdu 1011 Starship Troopers(树形DP入门)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 使用 C# 9 的records作为强类型ID - 路由和查询参数
上一篇文章,我介绍了使用 C# 9 的record类型作为强类型id,非常简洁 public record ProductId(int Value); 但是在强类型id真正可用之前,还有一些问题需要解 ...
- mac安装Navicat Premium Mac 12 破解版
参考:https://www.cnblogs.com/lyfstorm/p/11123159.html 激活后:
- Pycharm同时执行多个脚本文件
Pycharm同时执行多个脚本文件 设置Pycharm使它可以同时执行多个程序 打开Pycharm 找到Run,点击确认 点击Edit Configurations 右上角Allow parallel ...
- 【Java】单例模式(Singleton)
重新搞一波 复习巩固 简单记录 慕课网 Java工程师 文章目录 单例概述 设计模式 单例模式(Singleton) 参考资料 单例概述 Singleton Pattern 单例模式是Java中最简单 ...
- ctfhub技能树—信息泄露—备份文件下载—网站源码
打开靶机 查看网页内容 使用dirsearch进行扫描 命令如下 python3 dirsearch.py -u http://challenge-91f1f5e6a791ab02.sandbox.c ...
- linux系统图形化管理工具
webmin是一个非常好的图形化管理工具,提供了系统管理员对于linux系统的运维效率.对于那些记不住命令,新入门的新手真的是一个很好的工具呀,上图吧. 这是系统管理的首页,可以看到,CPU,内存.虚 ...
- 基于HBuilderX+UniApp+ColorUi+UniCloud 优宝库 开发实战(一)
1. 优宝库介绍 优宝库是基于阿里妈妈.淘宝联盟 淘宝商品Api,前端使用HBuilderX + UniApp + ColorUi,后端采用UniClound 精选淘宝商品进行推荐的App程序.下 ...
- Mac 禁用动画
# opening and closing windows and popovers defaults write -g NSAutomaticWindowAnimationsEnabled -boo ...
- 找不到:DarchetypeCatalog=local
设置IDEA Maven->Runner 界面的VM Options参数值为-DarchetypeCatalog=local 刷新项目Maven配置,在项目右边界面,重新引入Maven
- .htaccess 和 .user.ini
.htaccess 仅能用于apache下,并且内容严格,不能有错误行,如:GIF89a .user.ini php 5.3.0开始引入,可设置除了:PHP_INI_SYSTEM 外的其他,包括(PH ...