HD 1011 Starship Troopers(树上的背包)
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15457 Accepted Submission(s): 4152
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.
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
题意:n个房间m个士兵,接下来n行表示每个房间bug和收益,其中一个士兵能干掉20个bug,然后n-1行是房间之间连通情况,问m个人最大的收益
dp[i][j]表示根节点为i,j个士兵的收益,则dp[i][j] = max{dp[ i ][ j ], dp[ i ][j - k] + dp[ son[i] ][k] }
当一个房间的bug为0是也需要一个士兵,所以当m = 0时直接输出0
节点减一
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX = ;
int dp[MAX][MAX],vis[MAX],bug[MAX],p[MAX];
vector<int> son[MAX];
int n,m;
void dfs(int u)
{
vis[u] = true;
for(int i = bug[u]; i <= m; i++)
dp[u][i] = p[u];
int tot = (int) son[u].size();
for(int i = ; i < tot; i++)
{
int v = son[u][i];
if(vis[v])
continue;
dfs(v);
for(int j = m; j >= bug[u]; j--)
{
for(int k = ; k <= j - bug[u]; k++)
{
if(dp[u][j] < dp[u][j - k] + dp[v][k])
dp[u][j] = dp[u][j - k] + dp[v][k];
}
}
}
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == - && m == -)
break;
for(int i = ; i < n; i++)
{
scanf("%d%d", &bug[i], &p[i]);
bug[i] = (bug[i] + ) / ;
}
for(int i = ; i <= n; i++)
son[i].clear();
for(int i = ; i < n - ; i++)
{
int a,b;
scanf("%d%d", &a, &b);
son[a - ].push_back(b - );
son[b - ].push_back(a - );
}
if(m == )
printf("0\n");
else
{
memset(vis, false, sizeof(vis));
memset(dp, , sizeof(dp));
dfs();
printf("%d\n", dp[][m]);
} } return ;
}
HD 1011 Starship Troopers(树上的背包)的更多相关文章
- 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://blog.csdn.net/shuangde800 题目链接 : hdu-1011 题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-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 ...
- 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
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电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 ...
- hdu 1011(Starship Troopers,树形dp)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 021医疗项目-模块二:药品目录的导入导出-介绍poi类
我们使用的是.10版本 Apache POI - the Java API for Microsoft Documents,Apache POI 是用Java编写的免费开源的跨平台的 Java API ...
- [4]Telerik Grid 简单使用方法
1.columns <% Html.Telerik().Grid(Model) .Name("Orders") .Columns(columns => { //绑定列名 ...
- (转载)关于Apache 的两种工作模式
今天在查看服务器的时候,发现服务器http请求数 每天增长越来越多,在优化集群服务器的时候,查看到Apache 的工作模式是prefork,于是想到了worker 模式, 想暂时的把当前运行模式改成w ...
- 安装mysql-connector-python
安装mysql-connector-python 1.下载. wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connec ...
- g++编译总结
g++编译&&gdb调试&&coredump调试 一.编译注意细节 1.使用g++编译CPP文件如果用gcc编译C++源文件时,加以下选项:-lstdc++,否则使用了 ...
- Web API路由和动作选择
前言 本文描述ASP.NET Web API如何把一个HTTP请求路由到控制器的一个特定的Action上.关于路由的总体概述可以参见上一篇教程 http://www.cnblogs.com/aehyo ...
- Linux常用指令---tar | zip (解压缩)
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...
- ubuntu14.04上安装Mysql-5.7.11
先安装好操作系统 在Mysql官网上下载最新版的Ubuntu Linux专用的Mysql.我这里下载的是:mysql-server_5.7.11-1ubuntu14.04_amd64.deb-bu ...
- 20145222黄亚奇《Java程序设计》第10周学习总结
20145222 <Java程序设计>第10周学习总结 学习总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接 ...
- error C3872: "0xa0": 此字符不允许在标识符中使用
整理:这是因为直接复制代码的问题.0xa0是十六进制数,换成十进制就是160,表示汉字的开始. 解决办法:在报错的代码行检查两边的空格,用英文输入法的空格替换掉. 万恶的网络,万恶的word,这些无厘 ...