传送门:The Elder - HDU 5956 - Virtual Judge

Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom consists of N cities, numbered from east to west. The 1-th city, which is located to the east of others, is the capital. Each city, except the capital, links none or several cities to the west, and exactly one city to the east.
There are some significant news happening in some cities every day. The Elder wants to know them as soon as possible. So, that is the job of journalist frogs, who run faster than any other frog. Once some tremendous news happen in a city, the journalist in that city would take the message and run to the capital. Once it reach another city, it can either continue running, or stop at that city and let another journalist to transport. The journalist frogs are too young and simple to run a long distance efficiently. As a result, it takes L^2L2 time for them to run through a path of length L. In addition, passing message requires P time for checking the message carefully, because any mistake in the message would make the Elder become extremely angry.
Now you are excited to receive the task to calculate the maximum time of sending a message from one of these cities to the capital.

Input

The first line of input contains an integer t, the number of test cases. t test cases follow. For each test case, in the first line there are two integers N (N ≤ 100000) and P (P ≤ 1000000). In the next N-1 lines, the i-th line describes the i-th road, a line with three integers u,v,w denotes an edge between the u-th city and v-th city with length w(w ≤ 100).

Output

For each case, output the maximum time.

Sample

Inputcopy Outputcopy

3 6 10 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3 6 30 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3 6 50 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3

51 75 81 

Hint

In the second case, the best transportation time is:
•  The 2-th city: 16 = 4^2
•  The 3-th city: 72 = 4^2 + 30 + 5^2
•  The 4-th city: 9 = 3^2
•   The 5-th city: 36 = (3 + 3)^2
•   The 6-th city: 75 = (3 + 3)^2 +30 + 3^2
Consequently, the news in the 6-th city requires most time to reach the capital.

AC代码:

#include <bits/stdc++.h>
using namespace std;typedef long long int ll;const int N = 1e5 + 100, M = N << 1;int h[N], ne[M], e[M], w[M], idx;
int que[N];
ll a[N], dp[N], f[N], m, ans;ll power2(ll x) {return x * x;
}void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}void dfs1(int x, int fa = -1){for (int i = h[x]; ~i; i = ne[i]) {int u = e[i];if (u == fa) continue;a[u] = a[x] + w[i];dfs1(u, x);}
}void dfs(int x, int fa, int hh, int tt) {while (hh < tt && f[que[hh + 1]] - f[que[hh]] <= (a[que[hh + 1]] - a[que[hh]]) * (2 * a[x])) hh++;dp[x] = dp[que[hh]] + power2(a[x] - a[que[hh]]) + m;ans = max(ans, dp[x]);f[x] = dp[x] + power2(a[x]);while (hh < tt && (f[que[tt]] - f[que[tt - 1]]) * (a[x] - a[que[tt - 1]]) >= (f[x] - f[que[tt - 1]]) * (a[que[tt]] - a[que[tt - 1]])) tt--;int pre = que[++tt];  que[tt] = x;for (int i = h[x]; ~i; i = ne[i]) {int u = e[i];if (u == fa) continue;dfs(u, x, hh, tt);}que[tt] = pre;
}void solve() {int n;scanf("%d%lld", &n, &m);memset(h, -1, sizeof(int) * (n + 10)); idx = 0;for (int i = 1; i < n; i++) {int u, v, c;scanf("%d%d%d", &u, &v, &c);add(u, v, c);add(v, u, c);}que[0] = 0;ans = 0;dfs1(1, -1);dfs(1, -1, 0, 0);printf("%lld\n", ans - m);
}int main() {//ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int t = 1;scanf("%d", &t);while (t--) solve();return 0;
}

做这题的总结:

这题求从其他点到1号点的最长距离的最小值,先推出一个dp式子,
    看到式子一般只有数据结构,单调队列,斜率优化。
    这题既有平方又有一次方,导致斜率不能直接求,可以用dp[j] < dp[k], 到i的距离写出式子
    然后就可以分离出j, i。
    由于是树型dp,可以改成从1号点到其他点的距离,由于是一颗树,导致对于x,每颗子树维护的队列都是不同的
    就会需要回溯。
    最后还有记得初始化ans,wa了好几次了呜呜

hdu5956, The Elder (树型dp, 斜率优化)相关推荐

  1. 其他OJ 树型DP 选课

    在朱全民的PPT介绍的一个树型DP经典题,<选课>,中文题目,不结束 找了很久找到了可以提交的OJ,重庆八中 http://www.cqoi.net:2012/JudgeOnline/pr ...

  2. 【树型DP】BZOJ1564 二叉查找树(noi2009)

    标签: 二叉查找树 [题目描述] 已知一棵特殊的二叉查找树.根据定义,该二叉查找树中每个结点的数据值都比它左儿子结点的数据值大,而比它右儿子结点的数据值小. 另一方面,这棵查找树中每个结点都有一个权值 ...

  3. 【树型DP】加分二叉树

    问题 b: [树型DP]加分二叉树 时间限制: 1 Sec  内存限制: 64 MB 提交: 8  解决: 6 [提交] [状态] [讨论版] [命题人:admin] 题目描述 科技忽略了过程就是魔法 ...

  4. 二叉苹果树(树型DP+背包)

    二叉苹果树 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点).这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号 ...

  5. POJ3342 Party at Hali-Bula(树型DP求最大独立集+唯一解判断)

    题意: 公司参加聚会,要求员工不能和他的上司同时参加,求最多能参加几个人并且判断解是否唯一. 要点: 树型DP的经典题,用dp[u][1]表示选取u的最大值,dp[u][0]表示不选取u的最大值,容易 ...

  6. 虚树+树型DP SDOI2011消耗战

    <虚树+树型DP> SDOI2011消耗战 #include <iostream> #include <cstdio> #include <cstring&g ...

  7. BSOJ 2923:藤原妹红 MST+树型DP

    2923 -- [模拟试题]藤原妹红 Description 在幻想乡,藤原妹红是拥有不老不死能力的人类.虽然不喜欢与人们交流,妹红仍然保护着误入迷途竹林村民.由于妹红算得上是幻想乡最强的人类,对于她 ...

  8. 洛谷P3354 Riv河流 [IOI2005] 树型dp

    正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...

  9. hihocoder 1479 三等分 树型dp

    描述 小Hi最近参加了一场比赛,这场比赛中小Hi被要求将一棵树拆成3份,使得每一份中所有节点的权值和相等. 比赛结束后,小Hi发现虽然大家得到的树几乎一模一样,但是每个人的方法都有所不同.于是小Hi希 ...

  10. 蓝桥杯:生命之树【树型dp】

    之前本菜还没学树型dp的时候,下意识地认为这个东西很难,感觉这个东西结合了搜索+dp这两座算法界的大山必定很难,但万万没想到啊,这个东西很像我之前讲的记忆化搜索,甚至我认为,记忆化搜索的一个作用就是将 ...

最新文章

  1. Sublime Text 4首个稳定版发布:全新UI、多选项卡、支持GPU渲染
  2. Python写爬虫只需三步
  3. 《视觉SLAM十四讲》笔记(ch8)
  4. 文件传送到服务器的软件,远程服务器文件传输软件
  5. linux C 语言的 system
  6. python命令行运行django项目, can‘t open file ‘manage.py‘ 问题解决
  7. tomcat的url-pattern的源码分析
  8. java swing图书管理系统 java swing mysql实现的图书管理系统源码(1023)
  9. win7 开启梦幻桌面
  10. 王道计算机网络学习笔记
  11. 2022国开中国现代文学专题阶段作业2-4答案
  12. IDEA 设置前进,后退快捷键
  13. 《论文阅读》Generating Responses with a Specific Emotion in Dialog
  14. 钉钉考勤与企业系统对接
  15. MATLAB Error:错误使用sym>convertChar
  16. 2020Ti电赛体会与经验
  17. linux unicode utf8.h,linux shell下16进制 “\uxxxx” unicode to UTF-8中文
  18. 支持将树莓派转换成NAS存储系统
  19. Input Axis Mouse X is not setup.
  20. AnalyticDB分析型数据库

热门文章

  1. ubuntu18.04安装OpenCV3.4.12步骤及安装中遇到的一些问题
  2. 解决linux:docker-compose: Permission denied
  3. 马云说过的计算机名言,马云说过最洗脑10名言 马云经典语录大全
  4. 手机端 js禁止页面滚动
  5. 上海南京路步行街向全球征集标识Logo及吉祥物设计
  6. 小虾米闯江湖服务器维护中,小虾米闯江湖数据总结及中期注意事项一览
  7. java计算机毕业设计四六级在线考试系统源码+系统+数据库+lw文档+mybatis+运行部署
  8. rust下沉试密室怎么用_魔兽7.1考古任务黑鸦堡垒密室在哪 钥匙使用消失二楼铁门怎么开...
  9. vue学习之ElementUI时间选择器报错getTime is not a function
  10. Application loader:ERROR ITMS-90168: The binary you uploaded was invalid.