[Sdoi2011] fire


Time Limit: 10 Sec Memory Limit: 512 MB
entire country has n cities, any two of the n cities and there are unique communication path, each communication path length of two city zi (zi <= 1000).
People in this country have a passion beyond the universe to the flame, so the country's most thriving industry is the fire industry. As the government ran out of patience enthusiasm nationals (a large number of fire overhead expenses) but yet do nothing (the presidential campaign to support the national rate), we can only try ways to improve fire fighting capability.
Now this country funds sufficient to establish a fire on the hub side length s and not more than the path (city at both ends), in order to maximize the utilization of the hub, the distance to all other cities requires that the maximum value of minimum path .
You appointed to oversee the project, of course you need to know to establish a hub in what position.
Input
Input n rows:
row 1, two positive integers n and s, separated by a space. Wherein n is the number of cities, s is the path length of the boundary. In this set of nodes numbered 1,2, ......, n.
From the second line to the n-th line, each line separated by a space given positive integer 3 illustrates a sequence number and length of the two end points of each edge. For example, "247" represents the length of edges connecting the nodes 2 and 4 to 7.
Output
output contains a non-negative integer, i.e. the maximum of all the cities to the selected path, of course, the maximum value of all programs must be the smallest.
Sample Input
[1] Sample input
. 5 2
1 2. 5
2. 3 2
2. 4. 4
2. 5. 3

Sample input [2]
. 8. 6
. 1. 3 2
2 2. 3
. 3. 6. 4
. 4. 3. 5
. 4. 6. 4
. 4. 7 2
. 7. 8. 3
the Sample the Output

[1] Sample Output
5
[2] Sample Output

5
HINT

To 100% of the data, n <= 300000, 1000 is equal to the small side length.

 

/*
1 proof: for points on the diameter, from its farthest point must be a point on the diameter
Suppose diameter ac
        e
        .
        .
        . 
a.......b.....c(设ab>bc)
        .
		.
		.
		.
		d
For the point, b, from its furthest point should be a
If it is not a, but d, then (c, b, d) is the diameter should 
2 proved: For the non-point diameter d, the point farthest from it, must be a point on diameter
If not farthest from d a, but e.
The db + be> db + ab
Accordingly be> ab, that is to say from e b, b farther away than a, which began to prove contrary.
 
 
l...........t.......i.........r
l, r diameter of about endpoints, i is a point of our enumeration, t is another point (point i of the initial value of the father)
[T, i] does not exceed a predetermined distance Len
Diameter is not selected, the selected distance to the edge, as required as small as possible, so taking
years = min (years, max (d [T], d [r] -d [i]));
d arrays representing each point of the distance l. 
Since the tree all points from their farthest point, it must be one of the two end points of the diameter, so in general such a request can come out after
But taking into account the given Len may cover the entire route. Then find out ans 0
So we have to point out one by one on the diameter, the dot diameter does not obtain other to their distance
And then find the maximum of these distances, and then take a maximum ans. 

*/
 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int does [310100] vis [301000] last [301000] len = 0, d [301,000];
struct node
{
    int to,next,w;
}a[601000];
void add(int a1,int a2,int a3)
{
    as ++;
    and [only] .to = a2;
    a [len] .w = a3;
    a[len].next=last[a1];
    last [a1] = len;
}
void dfs(int x)
{
    for(int i=last[x];i;i=a[i].next)
    {
        int to=a[i].to;
        if(vis[to]||fa[x]==to) continue;
        fa[to]=x;
        d[to]=d[x]+a[i].w;
        dfs(to);
    }
}
int main ()
{   
    int n, s, x, y, z, years = 2147483647;
    cin>>n>>s;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    you l = 1, r = 1;
    dfs(l);
    for(int i=1;i<=n;i++)
    if(d[i]>d[l]) 
	    l=i;
    memset(fa,0,sizeof(fa));
    d[l]=0;
	dfs (l); // calculated from each point to the distance L, l is the diameter of the left end point 
    for(int i=1;i<=n;i++)
    if(d[i]>d[r]) 
	    r=i;
    int t = r; // find the right spot diameter 
    for(int i=r;i;i=fa[i])//尺取法
    {
        while(fa[t]&&d[i]-d[fa[t]]<=s)
		     for t = [t];
        years = min (years, max (d [T], d [r] -d [i]));
    }
    for (int i = r; i; i = fa [i]) // find the point on the diameter 
	    view [i] = 1;
    for(int i=r;i;i=fa[i])
	     d [i] = 0, dfs (i); // at each point on the diameter of the root, to find points on their distance to the diameter of the non- 
    for (int i = 1; i <= n; i ++) // find after these distances, the maximum value is obtained 
    if(vis[i]==0)
    years = max (years of [i]);
    cout << years;
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/11766421.html