[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值,只有当父节点被 ...
随机推荐
- POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)
题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...
- T-SQL
今天继续数据库知识的梳理.接下来的主要内容是T-SQL,针对的数据库是SQL Server 2008. 几个术语 数据定义语言(DDL,Data Definition Language):用来建立数据 ...
- 在Window IIS中安装运行node.js应用—你疯了吗
[原文发表地址]Installing and Running node.js applications within IIS on Windows - Are you mad? [原文发表时间]201 ...
- demo_01 css3中的radius
css属性:border-radius :border:边框:radius:弧度:所以这个属性的意思很明了. 下面实现一个小demo: <!doctype html> <html&g ...
- 客户端获取服务器SessionID (Asp.net SessionID)
SessionID是客户端首次访问某个方法或页面, 并且这个方法中设置了Session["xxx"]=xx; 此时服务器返回的响应头(HttpResponse.Headers)中会 ...
- H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
2015年的最后几天,移动开发圈里最为火爆的话题之一无疑是“iOS程序员月薪降至12K”这则报道. 有人认为这是O2O创业遇冷所致,也有人认为这是iOS生态过于封闭致使智能硬件等新领域对iOS开发者的 ...
- inline-block的兼容性问题
inline-block的兼容性问题 Inline-block是元素display属性的一个值.这个名字的由来是因为,display设置这个值的元素,兼具行内元素( inline elements)跟 ...
- php中的preg系列函数
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int ...
- Google地图数据算法
Google Maps与Google Earth中的每个级别的每一副图片都有一个URL,例如下面这幅我们学校的图的地址是http://kh.google.com/kh?v=3&t=trstrq ...
- c语言通过89C51驱动1602液晶显示(入门级别)
工具proteus,keil 步骤: 1.画好电路图 2.在指令模式下,设置好显示模式以及光标位置 3.在写数据模式下,向1602写入显示字符(1602只能显示数字和字符) 电路图 #include ...