题目链接

Problem Description

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 L2 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 Input

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

Sample Output

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.

Source

2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e5+10;
const int mod=100000000;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
struct node
{int v,w,next;
} maps[maxn<<1];
int head[maxn],tot;
int que[maxn],p,n,vis[maxn];
ll dis[maxn],dp[maxn],ans;
void init()
{me(head,-1),me(vis,0),me(dis,0);tot=0,ans=0,que[0]=0;
}
void add(int u,int v,int w)
{maps[tot].v=v;maps[tot].w=w;maps[tot].next=head[u];head[u]=tot++;
}
ll DY(int j,int k)
{return dp[j]+dis[j]*dis[j]-(dp[k]+dis[k]*dis[k]);
}
ll DX(int j,int k)
{return 2*(dis[j]-dis[k]);
}
ll DP(int i,int j)
{return dp[j]+(dis[i]-dis[j])*(dis[i]-dis[j])+p;//状态转移方程 dp[i]=min(dp[i],dp[j]+(dis[i]-dis[j])*(dis[i]-dis[j])+p);
}
void dfs(int u)//计算所有点到根节点的距离
{vis[u]=1;for(int i=head[u]; i!=-1; i=maps[i].next){int v=maps[i].v;if(!vis[v]){dis[v]=dis[u]+maps[i].w;dfs(v);}}
}
void DFS(int u,int fa,int s,int t)
{int pre=-1;while(s<t&&DY(que[s+1],que[s])<=dis[u]*DX(que[s+1],que[s]))s++;dp[u]=min(dp[u],DP(u,que[s]));while(s<t&&DY(que[t],que[t-1])*DX(u,que[t])>=DY(u,que[t])*DX(que[t],que[t-1]))t--;pre=que[++t];que[t]=u;ans=max(ans,dp[u]);for(int i=head[u]; i!=-1; i=maps[i].next){int v=maps[i].v;if(v!=fa)DFS(v,u,s,t);}if(pre!=-1)//因为u的子节点不止一个,下面还有其他节点。que[t]=pre;
}
int main()
{int t;scanf("%d",&t);while(t--){init();scanf("%d%d",&n,&p);for(int i=1; i<n; i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);add(u,v,w),add(v,u,w);}dfs(1);for(int i=1; i<=n; i++)dp[i]=dis[i]*dis[i];DFS(1,0,1,1);printf("%lld\n",ans);}return 0;
}

HDU 5956 The Elder(树型DP+斜率优化)相关推荐

  1. HDU 1520Anniversary party(树型DP)

    HDU 1520   Anniversary party 题目是说有N个人参加party,每个人有一个rating值(可以理解为权值)和一个up(上司的编号),为了保证party的趣味性,每一个人不可 ...

  2. 其他OJ 树型DP 选课

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. hihocoder 1479 三等分 树型dp

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

最新文章

  1. R语言t分布函数Student t distribution(dt, pt, qt rt )实战
  2. html表格筛选排序规则,excel表的排序功能你真的会吗?带你重新认识真正的排序功能...
  3. golang切片传参
  4. 【Linux】36.ubuntu删除vscode的缓存,可清理出几十G空间
  5. UltraEdit-64中文安装
  6. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL,spring获取context
  7. Binary String Minimizing CodeForces - 1256D(贪心)
  8. 在linux内核3.14.43添加自己的驱动源码,linux内核如何加入自己的驱动
  9. 智能指针auto_ptr源码
  10. 电子产品环境可靠性测试标准有哪些?
  11. gridviewnbsp;enableviewstate
  12. 【五年】Java打怪升级之路
  13. 优漫动游:如何解决Adobe XD无法拖入图片?
  14. Java毕业设计-美容院小程序管理系统
  15. PhotoShopcs6 文件格式关联
  16. 在ABAQUS中使用修正DPC模型
  17. 关于tensorly中的unfold
  18. 微信小程序开发笔记 进阶篇②——多个微信小程序一个用户体系,同一个UnionID
  19. 华为:业务决策,怎样才能让数字化真正指导行动?
  20. AvPlayer与AVPlayerItem

热门文章

  1. 强烈推荐提升自我的30个好习惯
  2. 如何避免在IE中执行window.close()后弹出一个新IE窗口
  3. 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(18):方阵的幂级数
  4. 简单的手机html页面源代码,手机页面h5的简单demo
  5. 湘潭大学 Hurry Up 三分,求凹函数的最小值问题
  6. 小明的爷爷108岁了,而我30岁才开始学编程
  7. 2021年山东省安全员C证考试题库及山东省安全员C证考试报名
  8. 冒险岛 PHP,php基础知识
  9. 虹科案例|光刻机汞灯替代者—定制大功率UV-LED平行光源
  10. java ftp上传文件 linux_linux下用java实现ftp上传、下载文件