洛谷P4408逃学的小孩

c o d e \color{orange}code code

#include 
#include 
#include 
#include 
#define maxn 444444
#define int long long
using namespace std ;
int n , m ;
int read() {
    int x = 0 , f = 1 ; char s = getchar() ;
    while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar() ;}
    while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar() ;}
    return x*f ; 
} 
int head[maxn] , vis[maxn] , fa[maxn] ;
struct dy{
    int x , y , z , next ;
}a[maxn] ;
int t ;
void add(int x ,int y ,int z) {
    a[++t].x = x ;
    a[t].y = y ;
    a[t].z = z ;
    a[t].next = head[x] ;
    head[x] = t ;
}
int deep[maxn] , dis[maxn] ;int maxx = -1 , tag ;
void dfs(int u,int fa) {
    for(int i = head[u] ; i ; i = a[i].next) {
        int v = a[i].y ;
        if(v == fa) continue ;
        dis[v] = dis[u] + a[i].z ;
        if(dis[v] > dis[tag]) tag = v ;
        dfs(v,u) ;
    }
}int D[maxn] ;
void Dfs(int u ,int fa) {
    for(int i = head[u] ; i ; i = a[i].next) {
        int v = a[i].y ;
        if(v == fa) continue ;
        D[v] = D[u] + a[i].z ;
        Dfs(v,u) ;
    }
}
signed main () {
    n = read() , m = read();
    while(m --) {
        int x = read() , y = read() , z = read() ;
        add(x,y,z) ;
        add(y,x,z) ;
    }dfs(4,0) ;
    int lc = tag ;
    memset(dis,0,sizeof(dis)) ;
    dfs(lc,0) ;
    int rc = tag ;
    int L = dis[rc] ;
    Dfs(rc,0) ;
    //cout << lc << "*" <
    for(int i = 1 ; i <= n ; i ++) {
        if(i == lc || i == rc) continue ;
        //cout << i << " " << D[i] <<" " << dis[i] <
        maxx = max(maxx,min(D[i],dis[i])) ;
    //	cout << maxx << endl ;
    }
    cout << maxx + L <<endl ;
    return 0 ;
}

你可能感兴趣的:(懒,懒)