[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值,只有当父节点被 ...
随机推荐
- HowTo: SVN undo add without reverting local changes
Reference: http://stackoverflow.com/questions/5083242/undo-svn-add-without-reverting-local-edits svn ...
- Mysql存储引擎__笔记
Mysql存储引擎(表类型): Mysql数据库: 通常意义上,数据库也就是数据的集合,具体到计算机上数据库可以使存储器上一些文件的集合或者一些内存 数据的内存数据的集合. Mysql数据库是开放源代 ...
- jQuery 1.7以后 jQuery2 新元素绑定事件on替代live
最近做了一个类别动态加载的功能,jQuery版本用的是2.02. 绑定事件jQuery1.7之前用的是live或者是bind.新版的jQuery新增了on方法 由于子类别是动态加载的,默认是不会有事件 ...
- 【转】c#文件操作大全(一)
1.创建文件夹//using System.IO;Directory.CreateDirectory(%%1); 2.创建文件//using System.IO;File.Create(%%1); 3 ...
- DOM五大对象
1.Window 对象:Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个 ...
- sae-多个file_put_contents('saestor://public/text.txt',$data);只写第一次
多个file_put_contents('saestor://public/text.txt',$data); 只执行第一个文件的写入,永久存储也只需要一次写入 如果需要用户中间缓存文件,用tmpfs ...
- POJ1384完全背包问题
题目大意:给你一个储蓄罐空的,和满的重量,然后给出各种硬币的价值和对应的重量,要你估计出储蓄罐里面硬币价值和最小为多少,注意要保证重量和恰好为给出满的重量解题思路:完全背包问题,只是求最小值,注意初始 ...
- 针对目前高校移动App的火热,哥决定点一把火
最近正在做市场调研,还请众位大哥大姐们帮忙投个票,求扩散 http://user.qzone.qq.com/717010686/vote/00000000feb6bc2a3ebd1e53
- 调用windows api 获取系统分辨率
c++中: int cxScreen,cyScreen; cxScreen=GetSystemMetrics(SM_CXSCREEN); cyScreen=GetSystemMetrics(SM_CY ...
- 加解密算法二:非对称加解密及RSA算法的实现
加密和解密使用不同的密钥的一类加密算法.这类加密算法通常有两个密钥A和B,使用密钥A加密数据得到的密文,只有密钥B可以进行解密操作(即使密钥A也无法解密):相反,使用密钥B加密数据得到的密文,只有密钥 ...