codeforce 1004 E. Sonya and Ice Cream(树的直径)

E. Sonya and Ice Cream
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.

Sonya lives in a city with nn junctions and n1n−1 streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets.

Sonya has exactly kk friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.

Sonya wants all her ice cream shops to form a simple path of the length rr (1rk1≤r≤k), i.e. to be located in different junctions f1,f2,,frf1,f2,…,fr and there is street between fifi and fi+1fi+1 for each ii from 11 to r1r−1.

The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions aa and bb is equal to the sum of all the street lengths that you need to pass to get from the junction aa to the junction bb. So Sonya wants to minimize

maxamin1irda,fimaxamin1≤i≤rda,fi

where aa takes a value of all possible nn junctions, fifi — the junction where the ii-th Sonya's shop is located, and dx,ydx,y — the distance between the junctions xx and yy.

Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than kk shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.

Input

The first line contains two integers nn and kk (1kn1051≤k≤n≤105) — the number of junctions and friends respectively.

Each of the next n1n−1 lines contains three integers uiuivivi, and didi (1ui,vin1≤ui,vi≤nviuivi≠ui1d1041≤d≤104) — junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other.

Output

Print one number — the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most kk.

Examples
input
Copy
6 2
1 2 3
2 3 4
4 5 2
4 6 3
2 4 6
output
Copy
4
input
Copy
10 3
1 2 5
5 7 2
3 2 6
10 6 3
3 8 1
6 4 2
4 1 6
6 9 4
5 2 5
output
Copy
7


思路:
所选的那条线一定在树的直径上,
所以选的线应该是直径上连续的一段。
以每个树的直径上的点为根,形成一颗
不包括其他直径上的点的子树。
第i个点形成的子树中离i最远的点的距离记为dep[i]。
取最大的dep[i]为max_dep
设树的直径两端点是L,R。
选的直线的两端点是l,r
枚举直线的位置,对于每种情况,最远距离既是
L~l的距离,r~R的距离,max_dep三者中的最大值。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node
{
    int to,cost,next;
}rode[2*maxn];
int n,k,d[maxn],fa[maxn],dep[maxn];
int h[maxn],deq[maxn],head[maxn],tol;
bool vis[maxn];
void add(int a,int b,int c)
{
    rode[tol].to=b;
    rode[tol].cost=c;
    rode[tol].next=head[a];
    head[a]=tol++;
}
void bfs(int st)
{
    queue<int>P;
    for(int i=0;i<maxn;i++)
        d[i]=1e9;
    P.push(st);d[st]=0;
    fa[st]=-1;
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=head[v];i!=-1;i=rode[i].next)
        {
            node e=rode[i];
            if(d[e.to]>d[v]+e.cost)
            {
                d[e.to]=d[v]+e.cost;
                fa[e.to]=v;
                P.push(e.to);
            }
        }
    }
}
void dfs(int v,int f)
{
    for(int i=head[v];i!=-1;i=rode[i].next)
    {
        node e=rode[i];
        if(vis[e.to]||e.to==f)continue;
        dfs(e.to,v);
        dep[v]=max(dep[v],dep[e.to]+e.cost);
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&k);
    for(int i=1;i<n;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    bfs(1);
    int id=1,st,en;
    for(int i=2;i<=n;i++)
        if(d[id]<=d[i]) id=i;
    en=id;
    bfs(id);
    for(int i=1;i<=n;i++)
        if(d[id]<=d[i]) id=i;
    int x=id;
    st=id;
    while(x!=-1)
    {
        vis[x]=1;
        x=fa[x];
    }
    x=id;
    while(x!=-1)
    {
        vis[x]=1;
        dfs(x,0);
        x=fa[x];
    }
    x=id;
    int y=0;
    while(x!=-1)
    {
        h[y++]=x;
        x=fa[x];
    }
    //cout<<d[st];
    int s=0,t=0,ans=1e9,h_max=0;
    for(int i=0;i<y;i++)
    {
         //while(s<t&&dep[h[deq[t-1]]]<=dep[h[i]]) t--;
         //deq[t++]=i;
         //while(i-k>=deq[s])s++;
         int x1,x2;
         if(i-k+1<0)x1=0;
         else x1=d[st]-d[h[i-k+1]];
         x2=d[h[i]];
         h_max=max(h_max,dep[h[i]]);
         ans=min(ans,max(h_max,max(x1,x2)));
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/81030455