HDU - 59562016ACM/ICPC Asia Shenyang Station I - The Elder tree slope optimization dp

The meaning of the question: Given the previous tree, then each edge has a weight, and then there are two distances from each point to 1. The first one is to go directly back to 1, and the cost is dist(1, i)^2 , there is another one is to go to another point j first, and then two go from j to 1, of course, j can also go to k again, all the way to 1, but after a point, then there will be an extra cost p, ask you What is the maximum value of the minimum distance from each point to 1.

Solution: dp[i] represents the minimum cost of starting from node 1 and passing it down to node i, dp[i]=min(dp[j]+dis[i]-dis[j])^2, j is from i to each point on the chain of 1, obviously the slope is optimized, dis[i] is the distance from i to the root node

dp[i]+2*dis[i]dis[j]= dis[j]+dis[i]*dis[i]+dis[j]*dis[j];

b  +   k      *  x  =   y

It should be noted that the head and last and q[last+1] of the previous layer should be recorded when dfs, because only the last covered last+1 is changed, which is convenient to return to the original state

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=100000+10,maxn=1000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;

vector<pair<int,ll> >v[N];
ll dis[N],dp[N],ans,P;
int q[N],head,last;
struct node{
    int head,last;
    int id,change;
}p[N];
inline ll x(int j)
{
    return dis[j];
}
inline ll y(int j)
{
    return dp[j]+dis[j]*dis[j];
}
inline double slope(int i,int j)
{
//    printf("------%d %d\n", i, j);
    return (y(i)-y(j))/(x(i)-x(j));
}
void dfsdis(int u,int f,ll val)
{
    dis[u]=val;
    for(int i=0;i<v[u].size();i++)
    {
        int x=v[u][i].fi;
        if(x!=f)dfsdis(x,u,val+v[u][i].se);
    }
//    printf("%d %d\n",u,dis[u]);
}
void dfs(int u,int f)
{
    for(int i=0;i<v[u].size();i++)
    {
        int x=v[u][i].fi;
        if(x!=f)
        {
//            for(int j=head;j<=last;j++)printf("%d ",q[j]);printf("%d\n",x);
            p[x].head=head,p[x].last=last;
            while(head<last&&slope(q[head],q[head+1])<2*dis[x])head++;
            int te=q[head];
            dp[x]=dp[te]+(dis[x]-dis[te])*(dis[x]-dis[te])+P;
//            printf("%lld %d %lld\n",P,x,dp[x]);
            ans=max(ans,dp[x]);
            while(head<last&&slope(q[last-1],q[last])>slope(q[last],x))last--;
            p[x].id=last+1,p[x].change=q[last+1];
            q[++last]=x;
            dfs(x,u);
            head=p[x].head,last=p[x].last;
            q[p[x].id]=p[x].change;
//            for(int j=head;j<=last;j++)printf("%d ",q[j]);printf("%d\n",x);
        }
    }
}
intmain ()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d%d",&n,&P);
        for(int i=1;i<=n;i++)v[i].clear();
        for(int i=1;i<n;i++)
        {
            int u,vv;ll w;
            scanf("%d%d%lld",&u,&vv,&w);
            v[u].pb(mp(vv,w));
            v[vv].pb(mp(u,w));
        }
        dfsdis ( 1 , - 1 , 0 );
        head=1,last=1;q[head]=1;dp[1]=-P;
        years = 0 ;
        dfs(1,-1);
        printf("%lld\n",ans);
    }
    return 0;
}
/***********************
3
6 10
1 2 4
2 3 5
1 4 3
4 5 3
5 6 3
***********************/
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303049&siteId=291194637