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 ...
随机推荐
- python学习笔记 | selenium各浏览器驱动下载地址
Chrome http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的chromedriver.exe 版本也不一样, ...
- CAN总线采样点测试
采样点是什么? 采样点是接受节点判断信号逻辑的位置,CAN通讯属于异步通讯.需要通过不断的重新同步才能保证收发节点的采样准确. 若采样点太靠前,则因为线缆原因,DUT外发报文尚未稳定,容易发生采样错误 ...
- for update语句锁机制问题
数据库小知识学习系列 问题: MySQL InnoDB中,select where xxx=123 for update:该xxx没有索引,是使用表锁还是全部数据加行锁? 答: InnoDB引擎(默认 ...
- LeetCode590. N叉树的后序遍历
题目 1 class Solution { 2 public: 3 vector<int>ans; 4 vector<int> postorder(Node* root) { ...
- 使用NIM Server网络半自动安装AIX系统
一.NIM配置 1.安装NIMServer前准备 1.1.配置IP地址 # ifconfig –a #检查当前IP地址# # smitty mktcpip #设置IP地址# 选择第一块网卡(插网线的网 ...
- git创建分支并关联远程分支
1.新建本地分支: 如图,再输入你的分支名字,然后选择从哪个远程分支拉代码,如选择master 至此本地分支创建完成. 2.关联远程分支: (1).先输入git branch -vv,看看分支与远程分 ...
- 1.5V升3.3V芯片电路图,稳压3.3V供电MCU模块等
干电池1.5V可以升到3.3V,通过PW5100干电池升压IC,于外围3个元件:2个电容和一个电感即可组成1.5V升3.3V的电路系统. 干电池属于低能量的电池产品,不过一般使用到干电池的产品也是输出 ...
- Docker下梦织CMS的部署
摘要:Docker的广泛应用相对于传统的虚拟机而言提高了资源的利用率,推广后docker的影响不容忽视,在启动速度.硬盘.内存.运行密度.性能.隔离性和迁移性方面都有很大的提高.本次实训我们在cent ...
- js模仿京东首页的倒计时功能
模仿京东首页的倒计时 我们学习了定时器,可以用定时器做一个倒计时,于是我模仿了京东首页倒计时. 先看看京东首页的倒计时. 思路: 如何倒计时? 给一个未来的时间.然后计算未来时间到现在的时间戳. 用定 ...
- 【Android】编译报错 Annotation processors must be explicitly declared now 解决方案
问题 在网上下载一个demo,因为版本久远,里面添加了本地 Butter Knife 的jar包,在编译时报错 Annotation processors must be explicitly dec ...