hdu1011 Starship Troopers
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14426    Accepted Submission(s): 3887
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.
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.
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
7
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 105
int first[maxn],vis[maxn],w[maxn],value[maxn];
int dp[maxn][maxn];
struct node{
    int to,next;
}e[2*maxn];
int n,m;
void dfs(int u)
{
    int i,j,v,t,k;
    vis[u]=1;
    int flag=0;
    if(w[u]%20==0)t=w[u]/20;
    else t=w[u]/20+1;
    for(i=t;i<=m;i++)dp[u][i]=value[u]; //这里要先初始化为value[u]
    for(i=first[u];i!=-1;i=e[i].next){
        v=e[i].to;
        if(vis[v])continue;
        flag=1;
        dfs(v);
        for(j=m;j>=t;j--){
            for(k=j-t;k>0;k--){       //这里k不能等于0,因为如果不派队伍,那么得到的概率一定是0
                dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
            }
        }
    }
    for(j=t-1;j>=0;j--){
        dp[u][j]=0;
    }
    if(flag==0){
        if(w[u]%20==0)t=w[u]/20;
        else t=w[u]/20+1;
        for(j=t;j<=m;j++)dp[u][j]=value[u];
    }
}
int main()
{
    int i,j,c,d,tot;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==-1 && m==-1)break;
        for(i=1;i<=n;i++){
            scanf("%d%d",&w[i],&value[i]);
        }
        memset(first,-1,sizeof(first));
        tot=0;
        for(i=1;i<=n-1;i++){
            scanf("%d%d",&c,&d);
            tot++;
            e[tot].next=first[c];e[tot].to=d;
            first[c]=tot;
            tot++;
            e[tot].next=first[d];e[tot].to=c;
            first[d]=tot;
        }
        if(m==0){
            printf("0\n");continue;
        }
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        dfs(1);
        printf("%d\n",dp[1][m]);
    }
    return 0;
}
hdu1011 Starship Troopers的更多相关文章
- 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 (树形DP+分组背包)
		
题目大意:给一棵有根带点权树,并且给出容量.求在不超过容量下的最大权值.前提是选完父节点才能选子节点. 题目分析:树上的分组背包. ps:特判m为0时的情况. 代码如下: # include<i ...
 - hdu1011 Starship Troopers 树形DP
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 思路:很明显的树形背包 定义dp[root][m]表示以root为根,派m个士兵的最优解,那么d ...
 - HD 1011 Starship Troopers(树上的背包)
		
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
 - Starship Troopers
		
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
 - [HDU 1011] Starship Troopers
		
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
 - HDU 1011 树形背包(DP) Starship Troopers
		
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
 - 杭电OJ——1011 Starship Troopers(dfs + 树形dp)
		
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...
 - hdu 1011 Starship Troopers(树形DP入门)
		
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
 
随机推荐
- 初识JWT
			
1.JWT是什么 官方网站 JWT是JSON Web Token的简称.是一种开放标准(RFC 7519),定义了一种紧凑且自包含的方式,以JSON对象的形式在各方之间安全地传输信息,因为他被数字签名 ...
 - innodb日志文件大小
			
innodb是用多个文件作为一组循环日志,通常不需要修改默认的日志数量,只修改每个日志文件的大小即可,要修改日志文件大小,需要完全关闭mysql,将旧的日志文件移到其他地方保存,重新配置参数,然后重启 ...
 - 修改机器的hostname
			
vi /etc/sysconfig/network hostname=你想设置的主机名 不重启器的情况下使显示名称变成 hostname 主机名
 - Java并发/多线程-CAS原理分析
			
目录 什么是CAS 并发安全问题 举一个典型的例子i++ 如何解决? 底层原理 CAS需要注意的问题 使用限制 ABA 问题 概念 解决方案 高竞争下的开销问题 什么是CAS CAS 即 compar ...
 - python sqlite3增加表字段
			
给sqlite3表格增加新字段,要注意大小写,要不然不成功. 一开始这样写,不成功! 后面规范写,按大小写严格规范写! 成功了!现在查看新增加的字段commit: 仔细看,这下全部小写,括表名称.co ...
 - LRU(Least Recently Used)最近未使用置换算法--c实现
			
在OS中,一些程序的大小超过内存的大小(比如好几十G的游戏要在16G的内存上跑),便产生了虚拟内存的概念 我们通过给每个进程适当的物理块(内存),只让经常被调用的页面常驻在物理块上,不常用的页面就放在 ...
 - 为什么不建议用var
			
看了这个例子估计你就会明白了 var a = 'global'; function test() { if (!a) { var a = 'part'; } console.log(a); } tes ...
 - Table controls and tabstrip controls
			
本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...
 - UI测试框架
			
1. 从上到下共分成4层: 用例层 组件管理层 元素管理层 公共数据层 2. 用例层: 将每条用例使用参数化, 公共参数存储到"公共数据层", 中间参数通过组件层传递 3. ...
 - 【Python】中国有哪些同名的省市县?
			
这道题适合写个脚本来解. 首先从百度地图API下载一份行政区划数据. 开发资源 | 百度地图API SDK 然后做一个简单的数据统计就可以啦~ 行政区划同一级同名的: import pandas as ...