题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003

Description
Tower defense is a kind of real-time strategy computer games. The goal of tower defense games is to try to stop enemies from reaching your bases by building towers which shoot at them as they pass.


The choice and positioning of the towers is the essential strategy of the game. Many games, such as Flash Element Tower Defense, feature enemies that run through a “maze”, which allows the player to strategically place towers for optimal effectiveness. However, some versions of the genre force the user to create the maze out of their own towers, such as Desktop Tower Defense. Some versions are a hybrid of these two types, with preset paths that can be modified to some extent by tower placement, or towers that can be modified by path placement.

geoDefense is a Thinking Man’s Action Tower Defense. It has become one of “PC World’s 10 iPhone Games You CANNOT Live Without”. Using exciting vectorized graphics, this highly kinetic game brings a whole new dimension to the defense genre. Devastate creeps with blasters, lasers and missiles and watch their energy debris swirl through the gravity wells of your vortex towers.

There is a geoDefense maze of n points numbered from 1 and connected by passageways. There are at least two dead ends among these n points, and there is always one and only one path between any pair of points. Point 1 is a dead end, and it’s the base of enemies, and all the other dead ends are your bases.

To prevent the enemy reaching your bases, you have to construct towers to attack the enemy. You can build tower on any point and you can only build one tower on one point. A tower can only shot the enemy when it passes the tower. You are given ki choices to build tower on point i, and each choice is given in the format of (price, power) which means that you can build a tower with attack power value equals power in the cost of price. You can also build nothing on a point so it will not cost your money. A tower will reduce the enemy’s HP by its attack power. When the HP is less or equal to zero, the enemy dies immediately.

The base of enemies will release only one enemy. It moves very fast that you cannot do anything such as building towers while it is running. It runs all the way until it dies or reaches one of your bases. However, you cannot predict the route it will go through. To win the game, you must kill the enemy before it reaches your bases. You have to strategically place towers for optimal effectiveness so that the fortifications are steady enough to protect the bold and powerful enemy with high HP. You are troubling your head on figuring out the highest HP of the enemy you are able to kill on the way certainly. You have money m when the game begins.
Please note that the towers build in the enemy’s base or your bases are all effective and if the enemy is shot to death in your bases, you still win.

Input

The input consists of several test cases. The first line is an integer T (1 <= T <= 20), which shows the number of the cases.
For each test case, the first line contains only one integer n (2 <= n <= 1000) meaning the number of points.
The following n-1 lines describe the passageways. Each line contains two integers u and v, which are the endpoints of a passageway.
The following line contains only one integer m (1 <= m <= 200) meaning the amount of your money when the game begins.
Then n lines follow. The ith line describes the construction choices of the ith point. It starts with an integer ki (0 <= ki <= 50) and ki is followed by ki pairs of integers separated by spaces. The jth pair is (pricei,j, poweri,j), 0 <= pricei,j <= 200, 0 <= poweri,j <= 50000. ki being zero means that you can’t build a tower on the ith point.

Output
For each test case, output a line containing the highest HP value of your enemy that you can deal with. It means that if your enemy’s HP is larger than that highest value, you can’t guarantee your victory.

Sample Input
2
2
1 2
30
3 10 20 20 40 30 50
3 10 30 20 40 30 45
4
2 1
3 1
1 4
60
3 10 20 20 40 30 50
3 10 30 20 40 30 45
3 10 30 20 40 30 35
3 10 30 20 40 30 35

Sample Output
70
80

题意
给出n个节点的树和金钱m,每个节点可以建造一些炮塔,每个炮塔有价格和伤害,有怪物从点1出发,会到达任意叶子节点,问从点1分别到每个叶子节点一路上造成总伤害的最小值最大是多少。

吐槽
网上很多大牛的题解我看完都不懂…
感觉自己很弟弟:)

思路
树形dp+分组背包+01背包。对于每个节点,只有建和不建。dp[i][j]为以i为根节点的子树花费j元得到的最小值最大。先处理不建炮台的所有情况,tmp[j]为该点不建炮台时子节点花费j元得到的最小值最大,因为tmp[j]只会跟dp[i的子节点]有关,所以可以先处理出来。tmp是滚动的,这时就是一个分组背包。再处理建炮台的情况,对于在i节点建炮台,则是一个01背包,每种炮台建或不建。建则是tmp[j-炮台的价格]+炮台的伤害,因为tmp是不建炮台的情况。


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
#include<iostream>#include<cstdio>#include<vector>#include<cstring>#include<string>#include<algorithm>#include<set>#include<cstdlib>#include<map>#include<queue>#include<cmath>using namespace std;typedef long long ll;const int maxn = 2000;const int inf = 2e9;struct node {    int s, e, next;}edge[maxn * 2];struct tt {    int w, c;}a[maxn][maxn];int head[maxn], dp[maxn][210], tmp[maxn];int len, n, m;void add(int s, int e) {   edge[len].s = s; edge[len].e = e; edge[len].next = head[s];    head[s] = len++;

}void init() {   memset(head, -1, sizeof(head));   memset(dp, 0, sizeof(dp));    len = 0;}void  dfs(int x, int fa) {  for (int i = head[x]; i != -1; i = edge[i].next) {     int y = edge[i].e;       if (y == fa)            continue;     dfs(y, x);    } for (int i = 0; i <= m; i++)tmp[i] = inf; for (int i = head[x]; i != -1; i = edge[i].next) {     int y = edge[i].e;       if (y == fa)            continue;     for (int j = m; j >= 0; j--) {           tmp[j] = min(tmp[j], dp[y][0]);          for (int k = j; k >= 0; k--) {               tmp[j] = max(tmp[j], min(tmp[k], dp[y][j - k]));         }     } } for (int i = 0; i <= m; i++) {     if (tmp[i] == inf)          tmp[i] = 0;      dp[x][i] = tmp[i];   } for (int i = 1; i <=a[x][0].c; i++) {      int w = a[x][i].w;       int c = a[x][i].c;       for (int j = m; j >= w; j--) {           dp[x][j] = max(dp[x][j], tmp[j - w] + c);       } }}int main() {    int t;    scanf("%d", &t);    while (t--) {     scanf("%d", &n);        init();       int x, y;     for (int i = 1; i < n; i++) {           scanf("%d%d", &x, &y);          add(x, y);            add(y, x);        }     scanf("%d", &m);        for (int i = 1; i <= n; i++) {         scanf("%d", &a[i][0].w);            a[i][0].c = a[i][0].w;           for (int j = 1; j <= a[i][0].w; j++)               scanf("%d%d", &a[i][j].w, &a[i][j].c);      }     dfs(1, -1);       printf("%d\n", dp[1][m]);   }}

谢谢你请我吃糖果

hdu4044-树形dp相关推荐

  1. BNUOJ 52305 Around the World 树形dp

    题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52305 Around the World Time Limit: 20000msMemory ...

  2. [树形dp] Jzoj P5233 概率博弈

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵n个点的以1为根的有根树,叶子有权值.假设有m个叶子,那么树上每个叶子的权值序列就是一个1->m 的排列. 一开始在1号点有 ...

  3. fwt优化+树形DP HDU 5909

    1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include ...

  4. BZOJ 1040 ZJOI2008 骑士 树形DP

    题目大意:给定一个基环树林,每一个点上有权值,要求选择一个权值和最大的点集,要求点集中的随意两个点之间不能直接相连 最大点独立集--考虑到n<=100W,网络流铁定跑不了,于是我们考虑树形DP ...

  5. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  6. [NC15748]旅游 树形dp基础

    菜鸡第一次接触树形dp这个东西,不过这个东西还是很好理解的(可能是因为模板题吧) 个人感觉,相比线性dp,树形dp的状态转移方程更加的直观,难点主要是在"树"的结构上比较麻烦. 题 ...

  7. 容斥 + 树形dp ---- 2021 icpc 沈阳 L Perfect Matchings

    题目链接 题目大意: 就是给你一个2n2n2n个点的完全图,从这个图里面抽出2n−12n-12n−1条边,这些边形成一颗树,现在问你剩下的图里面点进行完美匹配有多少种方案? 解题思路: 一开始被完美匹 ...

  8. 树形dp ---- gym101667 A(贪心 + 树形dp + 两个dp方程组维护)

    题目链接 题目大意: 就是一棵5e35e35e3的树,可以选择一些点,放上基站,如果uuu上的基站价值为ddd,那么距离uuu小于等于ddd的点都会被覆盖,问使得整棵树被覆盖需要的最小价值. 解题思路 ...

  9. 树形dp ---- 2018年杭电多校第二场 H travel

    题目大意: 就是给你一个带点权的树,找到3条独立互不相交的路径使得权值和最大 解题思路: 很经典的树形dp 我们设dp[root][j][k]dp[root][j][k]dp[root][j][k]表 ...

  10. 2019 GDCPC or HDU6540 树形dp[计数dp] 详解

    题目链接 题目大意: 就是给你一颗nnn个点的树,树上有mmm个关键点,你可以选择若干个关键点组成集合SSS,这个集合满足任意两点在树上的距离不超过kkk,问你有多少种选法? 解题思路: 我们考虑树形 ...

最新文章

  1. android基础ui控件,Android基础——基础UI控件
  2. sklearn快速入门教程:(一)准备工作
  3. Flink开发环境搭建(maven)
  4. 送你了,思科设备基础配置命令大全(一),赶紧收藏......
  5. 京东JDHBase异地多活实践
  6. 疯子的算法总结(七) 字符串算法之 manacher 算法 O(N)解决回文串
  7. totolinkn200up怎么设置_totolinkN200R无线路由器如何设置啊,求高人指点
  8. Pytest之参数化
  9. python编写命令行框架_使用 Python 和 Click 编写命令行应用程序
  10. 安全模式:天猫 App 启动保护实践
  11. 萌萌机器人布娃娃图片_萌萌机器人教程
  12. 分区桌面 壁纸(正在做,常用,之后在做,临时存)
  13. STM32F103如何使用串口下载程序
  14. 微信小程序PDF下载方案
  15. 对称加密算法基本介绍
  16. 肠道重要菌属——Akkermansia Muciniphila,它如何保护肠道健康
  17. python递归排列组合_Python 排列组合
  18. Cornerstone 4.1 Mac破解版
  19. 【OpenGL学习笔记⑤】——纹理变换【glm配置+两张图片交替渐变变换 + 纹理平移 + 实现雪花飘落】
  20. ThreeJS加载geojson数据实现3D地图

热门文章

  1. 第二章 数据采集模块之SpringBoot埋点数据采集(源码 资料见文末)
  2. c语言学生成绩单,c语言-学生成绩单制作
  3. 牛客网C语言入门刷题(BC1 ~ BC50)
  4. 前端怎么把文件加密之后传给后端
  5. iOS 定位的时候没有出显弹框选择
  6. C语言程序设计基本运算符,C语言程序设计2第4章基本运算符和表达式.ppt
  7. 数字低通滤波器的原理及实现
  8. python执行脚本失败恢复环境_Textmate 执行python脚本的错误:env: python3: No such file or directory...
  9. YUV YPbPr YCbCr CCIR 601 CCIR 656
  10. css魔幻属性跟进篇