hdu5956 树上斜率DP
来源:互联网 发布:网易邮箱大师mac版 编辑:程序博客网 时间:2024/05/16 08:06
The Elder
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 934 Accepted Submission(s): 249
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 takesL2 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.
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
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
36 101 2 42 3 51 4 34 5 35 6 36 301 2 42 3 51 4 34 5 35 6 36 501 2 42 3 51 4 34 5 35 6 3
Sample Output
517581HintIn 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^2Consequently, the news in the 6-th city requires most time to reach the capital.#include<bits/stdc++.h>using namespace std;typedef long long LL;const int N =100011;int k;int head[N*2];struct edg{ int u,v,w,next;};edg E[N*2];void add(int u,int v,int w){ E[k].u=u; E[k].v=v; E[k].w=w; E[k].next=head[u]; head[u]=k++; E[k].v=u; E[k].u=v; E[k].w=w; E[k].next=head[v]; head[v]=k++;}LL dp[N];LL sum[N];int q[N];LL ans;int n,m;LL getup(int i,int j){ return dp[i]+sum[i]*sum[i]-(dp[j]+sum[j]*sum[j]);}LL getdown(int i,int j){ return 2*(sum[i]-sum[j]);}LL getdp(int i,int j){ return dp[i]+m+(sum[j]-sum[i])*(sum[j]-sum[i]);}void dfs(int u,int f,int s,int e){ int pre=-1; dp[u]=sum[u]*sum[u]; while(s<e&&getup(q[s+1],q[s])<=sum[u]*getdown(q[s+1],q[s])) s++; dp[u]=min(getdp(q[s],u),dp[u]); while(s<e&&getup(u,q[e])*getdown(q[e],q[e-1])<=getup(q[e],q[e-1])*getdown(u,q[e])) e--; pre=q[++e]; q[e]=u; ans=max(ans,dp[u]); for(int i=head[u]; i!=-1; i=E[i].next) { int v=E[i].v; int w=E[i].w; if(v!=f) { sum[v]=sum[u]+w; dfs(v,u,s,e); } } if(pre!=-1) q[e]=pre;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); k=0; memset(head,-1,sizeof(head)); for(int i=1; i<n; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); } sum[1]=0; q[0]=0; ans=0; dfs(1,0,1,0); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- hdu5956 树上斜率DP
- HDU5956 The Elder(树上斜率DP)
- bzoj1767 树上dp斜率优化+二分
- bzoj3672 [ NOI2014 ] -- 树上CDQ分治 + 斜率优化DP
- HDU 5956 The Elder(树上斜率优化DP)
- HDU 5956 树上进行斜率优化DP + 记录操作并撤销
- BZOJ3672:[Noi2014]购票 (斜率优化DP+二分+(树上CDQ分治/树链剖分))
- 斜率dp
- DP(斜率优化)
- 【斜率优化DP】Batch_Scheduling
- dp优化--斜率
- 斜率优化DP
- 斜率优化DP
- hdu3507斜率优化dp
- DP斜率优化总结
- hdu3480 斜率优化dp
- hdu3045之斜率DP
- hdu3507 斜率优化dp
- 软件工程_项目需求分析
- java for循环与if语句结合 通过辗转相除求 最大公约数和最小公倍数
- Spring 编程式事务管理
- "net start mysql"启动MySQL服务报错,提示发生系统错误5解决方法
- 使用缓冲字节流:BufferedInputStream与BufferedOutputStream读写数据
- hdu5956 树上斜率DP
- ionic多重路由
- 5.移动端事件--event 对象
- Ubuntu交叉编译U-boot
- [ACM]CCF CSP [201612-5]E题 卡牌游戏【75分的程序】
- 【OneNote 小技巧】——利用OneNote进行录音和录像
- MySQL与Oracle 差异比较之二基本语法
- Leetcode 107
- 用C语言实现的学生管理系统