hdu 1011 Starship Troopers(树上背包)
Problem Description
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 last test case is followed by two -1's.
Output
Sample Input
Sample Output
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=+;
const int maxm=+; vector<ll>edges[maxn]; ll f[maxn][maxm]; ll n,m;
ll v[maxn],c[maxn]; void addedge(ll u,ll v){
edges[u].push_back(v);
edges[v].push_back(u); } void clear_(ll n){
for(ll i=;i<=n;i++)edges[i].clear();
memset(f,,sizeof(f));
} void dp(ll x,ll fa){
for(ll i=;i<(ll)edges[x].size();i++){
ll y=edges[x][i];
if(y!=fa){
dp(y,x);
for(ll t=m;t>=;t--){
for(ll j=t;j>=;j--){
if(t-j>=){
f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
}
}
}
}
}
for(ll t=m;t>;t--){
if(t>=c[x])f[x][t]=f[x][t-c[x]]+v[x];
else f[x][t]=;
}
} int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
if(n==m&&m==-)break;
for(int i=;i<=n;i++){
scanf("%lld%lld",&c[i],&v[i]);
c[i]=(c[i]+)/;
} clear_(n);
for(int i=;i<n;i++){
ll u,v;
scanf("%lld%lld",&u,&v);
addedge(u,v);
}
dp(,);
printf("%lld\n",f[][m]);
}
return ;
}
hdu 1011 Starship Troopers(树上背包)的更多相关文章
- hdu 1011 Starship Troopers(树形背包)
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
http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...
- 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 ...
- HD 1011 Starship Troopers(树上的背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1011 Starship Troopers (树dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...
- [HDU 1011] Starship Troopers
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) ...
随机推荐
- 如何更改Jframe里Jpanel的大小
先贴代码: package xiya; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; ...
- 软件质量保障初探_Chris
关于软件质量保障的体会 首先,软件质量保障的重要性不言而喻,书中说软件质量体现在以下方面 软件开发过程的可见性 软件开发过程的风险控制 软件内部模块,项目中间阶段的交付质量,项目管理工具的因素 软件开 ...
- Codeforces_729_C
http://codeforces.com/problemset/problem/729/C 二分找最小容量,然后找符合的最小花费. #include<iostream> #include ...
- SpringBoot项目版本升级:从1.5.3升级到2.1.8版本
SpringBoot项目版本升级:从1.5.3升级到2.1.8版本 前言 简单记录一次本人在自己的SpringBoot项目project-template中,把1.5.3版本升级到2.1.8版本时升级 ...
- Ansible:roles初始化系统
简介 本文介绍ansible的roles,通过roles来实现系统的初始化,其相当于将ansible的playbook拆分.本文通过Jenkins,传参,调用playbook来初始化系统. Githu ...
- [译]课程 3: 更多关于 Jobs 和 JobsDetails
译者注: 目录在这 [译]Quartz.NET 3.x 教程 译者注: 原文在这 Lesson 3: More About Jobs & JobDetails 正如你在 课程 2 中看到的, ...
- k8s系列---Service之ExternalName用法
需求:需要两个不同的namespace之间的不同pod可以通过name的形式访问 实现方式: A:在其他pod内ping [svcname].[namespace] ping出来到结果就是svc的ip ...
- Zookeeper 介绍 原理
简介: ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务. 它Google的Chubby一个开源的实现,在分布式协调技术方面做得比较好的就是Google的Chubby还有 ...
- 使用mysqlfrm恢复frm表结构的方法
1.mysqlfrm安装 由于mysqlfrm是mysql-utilities工具一部分,那么我们安装mysql-utilities即可,下载好对应的源码包,进行编译安装 shell> wget ...
- C语言快速排序函数------qsort();
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> ty ...