Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8540    Accepted Submission(s): 2379

Problem Description
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some 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.
 
Input
The input contains several test cases. The first line of each test case contains two integers N (0 < N <= 100) and M (0 <= M <= 100), which are the number of rooms in the cavern and the number of starship troopers you have, respectively. The following N lines 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.
 
Output
For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.
 
Sample Input
5 10
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
 
Sample Output
50
7
 
Author
XU, Chuan
 
Source
 
题意:消灭怪物,获取大脑。==》 花费wi ,得到价值val,是一颗树,入口是1.
     没有告诉你谁是根节点,输入顺序也没有说,谁是谁的父亲节点。
 
思路:构建双向图,然后在搜索的时候用一个use[]标记是否该节点出现过,来避免死循环的出现。
         这样图就能建立起来了。
         谁是根节点呢,其实,在一颗树,你以任一点为一棵树,都不会破坏,边的关系。
         所以直接从入口出发。1.
       
         状态转移方程:dp[ k ] [ j ] 代表着以 k 为根节点,用了 k 个士兵 得到的最大价值。
         题意已经告诉我们,j的最大值是M 。
         如何初始化? 这个很关键,而且这道题,存在依赖关系。
         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.
         初始化: int num=(w[k]+19)/20;      for(i=num;i<=m;i++) dp[ k ] [ i ]=val[ k ];
         通式:     i f (  dp[ k ] [ j-s ] !=-1  ) //必须要满足父亲节点里的  怪物  被消灭呀。
                  dp[ k ] [ j ]= max(   dp[ k ] [ j ], dp[ k ] [ j-s ]+ dp [ t ] [ s ]  )      
   
 
         数据会出现几个特例,
         wi vi = 0  0  这使得我们要用初始化 memset(dp,-1,sizeof(dp));
         if( M==0 )  printf("0\n");
         ...

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,m;
int w[],val[];
struct node
{
int next[];
int num;
}f[];
int dp[][];
bool use[]; int Max(int x,int y)
{
return x>y? x:y;
} void dfs(int k)
{
int i,j,t,s;
use[k]=true;
int num=(w[k]+)/;
for(i=num;i<=m;i++) dp[k][i]=val[k]; for(i=;i<=f[k].num;i++)
{
t=f[k].next[i];
if(use[t]==true) continue;
dfs(t);
for(j=m;j>=num;j--)
{
for(s=;s<=j;s++)
{
if(dp[k][j-s]!=-)
dp[k][j]=Max(dp[k][j],dp[k][j-s]+dp[t][s]);
}
}
}
} int main()
{
int i,x,y;
while(scanf("%d%d",&n,&m)>)
{
if(n==-&&m==-)break;
memset(dp,-,sizeof(dp));
memset(use,false,sizeof(use)); for(i=;i<=;i++)
f[i].num=; for(i=;i<=n;i++)
scanf("%d%d",&w[i],&val[i]);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
f[x].num++;
f[x].next[f[x].num]=y; f[y].num++;
f[y].next[f[y].num]=x;
}
if(m==)
{
printf("0\n");
continue;
}
dp[][m]=;
/*
1 1
21 1 --> 0
*/
dfs();
printf("%d\n",dp[][m]); }
return ;
}
 
 
 
 
 

hdu 1011 Starship Troopers 经典的树形DP ****的更多相关文章

  1. 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 ...

  2. hdu 1011 Starship Troopers(树形DP入门)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1011 Starship Troopers 树形背包dp

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu 1011(Starship Troopers,树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 1011 Starship Troopers 树形DP 有坑点

    本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...

  6. HDU 1011 Starship Troopers星河战队(树形dp)

    题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 ...

  7. hdu 1011 Starship Troopers(树形背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. [HDU 1011] Starship Troopers

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1011 Starship Troopers(树上背包)

    Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...

随机推荐

  1. bzoj2662冻结

    题目链接 话说为什么出题人老是卡$SPFA$啊$qwq$ 然而$SPFA$硬是让本宝宝写成了$dij$ 分情况讨论就好 /*************************************** ...

  2. java内存模型(jMM)(二)

    volatile关键字 volatile是一个类型修饰符(type specifier),就像大家更熟悉的const一样,它是被设计用来修饰被不同线程访问和修改的变量.volatile的作用是作为指令 ...

  3. 977 AlvinZH过生日(背包DP大作战S)

    977 AlvinZH过生日 思路 难题.逆推DP. 要明确dp的状态只与是否有选择权有关,而与选择权在谁手里无关.因为不论选择权在谁手里,那个人都会尽可能的获得最大的蛋糕重量. dp[i]表示分配到 ...

  4. 前端之JS的线程(最易懂)

    1. 前言 很多文章在介绍线程以及线程之间的关系,都存在着脱节的现象.还有的文章过于广大,涉及到了内核,本文希望以通俗易懂的话去描述晦涩的词语,可能会和实际有一丢丢的出入,但是更易理解. 我们都知道J ...

  5. 批量自动化生成PDF目录标签

    所需软件: 1. FreePic2Pdf(网上很容易找到) 2. python3 3. 天若OCR 链接:https://pan.baidu.com/s/1B9dUr3gc0pv0BSHo5QYGsQ ...

  6. error C2955: “std::xx”: 使用 类 模板 需要 模板 参数列表

    一般出现这个错误 最可能是一种情况 queue q;//这样写 这样写肯定错 [笑哭] queue<int> q; //正确的 我想静静了

  7. unity 渲染第二步

    先不要用 unity shader 提供给你的转换矩阵,看看屏幕上的图形,你会学到更多. --- <unity 渲染箴言> 假设你 create 了一个 cube,放在默认的位置,默认的 ...

  8. 通过overflow: scroll;来实现部分区域的滚动

    在移动端中,我们希望元素的滚动,可以通过一些插件的使用来实现滚动,当然也可以自己来实现. 比如:对于某一个区域,我们可以限制好高度之后,设定:overflow-y: scroll; 这样,就可以实现滚 ...

  9. springboot和quartz整合实现动态定时任务(持久化单节点)

    Quartz是一个完全由java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制,它支持定时任务持久化到数据库,从而避免了重启服务器时任务丢失,支持分布式多节点,大大的 ...

  10. Oracle 维护数据的完整性 一 约束

    简介:约束用于确保数据库满足特定的商业规则.在Oracle中,约束包括以下几种: 1.not null      非空约束       该劣质不能为null 2.unique       唯一约束   ...