hdu5956 树上斜率DP

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 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.
#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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值