[HDU 1011] Starship Troopers
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11476 Accepted Submission(s): 3166
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.
树形背包入门题、注意理解
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; #define N 110
struct Edge
{
int next,to;
}edge[N*N/];
int head[N<<],tot; int n,m;
int p[N];
int vis[N];
int bugs[N];
int dp[N][N]; void dfs(int u)
{
vis[u]=;
int i,j,k,v;
int r=bugs[u]%?bugs[u]/+:bugs[u]/;
for(i=r;i<=m;i++)
{
dp[u][i]=p[u];
}
for(i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].to;
if(!vis[v])
{
dfs(v);
for(j=m;j>=r;j--)
{
for(k=;k<=j-r;k++)
{
dp[u][j]=max(dp[u][j],dp[u][j-k]+ dp[v][k]);
}
}
}
}
}
void add(int x,int y)
{
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
}
int main()
{
while(scanf("%d%d",&n,&m), (n+)||(m+))
{
init();
for(int i=;i<=n;i++)
{
scanf("%d%d",&bugs[i],&p[i]);
}
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
if(m==) cout<<<<endl; //0个士兵,一无所获
else
{
dfs();
cout<<dp[][m]<<endl;
}
}
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
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/有依赖的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/Others) ...
- hdu 1011 Starship Troopers(树上背包)
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
- [HDU 1011] Starship Troopers (树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 dp[u][i]为以u为根节点的,花了不超过i元钱能够得到的最大价值 因为题目里说要访问子节点必 ...
- HDU 1011 Starship Troopers 树形+背包dp
http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...
随机推荐
- Android NDK 环境搭建 + 测试例程
懒得废话一大堆概念,关于ADT.NDK的概念要是你不懂,怎么会搜到这里来?所以你只需要根据下面的步骤来,就可以完成NDK环境搭建了. 步骤:(假设你未安装任何相关开发工具,如果已经安装了,就可以跳过) ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- MailOtto 实现完美预加载以及源码解读
背景: 最近项目组需要一个小课题分享,小白刚好从微博里看到一个这样有趣的开源工具MailOtto,是阿里巴巴员工 Drakeet 维护的一个专注懒事件的事件总线,gitHub地址为:https://g ...
- sql 自定义排序
方法一: 比如需要对SQL表中的字段NAME进行如下的排序: 张三(Z) 李四(L) 王五(W) 赵六(Z) 按照sql中的默认排序规则,根据字母顺序(a~z)排,结果为:李四 王五 赵六 张三 ...
- 恶心的学校机房SQL安装
学校机房每台PC(DELL OPTIPLEX 380)上有两个系统,分别对应XP中英文版.管理员将500G硬盘分为两部分(两个主分区,两个逻辑分区),每个系统占用一个主分区和一个逻辑分区,主分区都有冰 ...
- Sql Server 判断表或数据库是否存在
发布:thebaby 来源:脚本学堂 [大 中 小] 本文详细介绍了,在sql server中判断数据库或表是否存在的方法,有理论有实例,有需要的朋友可以参考下,一定有帮助的.原文地址:h ...
- 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ...
- Qt 5 如何修改打包好的应用程序图标
修改的方法是:首先准备个ICO图标.例如:A.ico,网上有很多图标文件.用记事本新建个txt里面就写一行:IDI_ICON1 ICON DISCARDABLE "A.ico" 保 ...
- ko list and css gradient
<!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...
- 使用KVC
KVC是Key Value Coding的简称,意思是键值编码,号称Cocoa的大招.它是一种可以直接通过字符串key(对象在名称)来访问或修改对象属性的机制. 使用 1.利用KVC可以随意修改一个对 ...