poj 2631 Roads in the North (树的直径)
poj2631 题意:一棵树中求两个点的最远距离。。。 思路:就是求树的直径。。。裸体。。。。1A
/* ***********************************************
Author :111qqz
Created Time :2016年07月12日 星期二 13时03分39秒
File Name :code/poj/2631.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E4+7;
int n,m;
vector < pi> edge[N];
int lst;
int ans;
int d[N];
bool vis[N];
void bfs( int s)
{
ms(d,0x3f);
ms(vis,false);
queue<int>q;
q.push(s);
vis[s] = true;
d[s] = 0 ;
while (!q.empty())
{
int u = q.front() ; q.pop();
lst = u ;
// cout<<"u:"<<u<<endl;
int siz = edge[u].size();
for ( int i = 0 ; i < siz ; i++)
{
int v = edge[u][i].fst;
if (vis[v]) continue;
vis[v] = true;
d[v] = d[u] + edge[u][i].sec;
ans = max(d[v],ans);
q.push(v);
}
}
int mx = 0;
for ( int i = 1 ; i <= n ; i++)
{
// cout<<"i:"<<i<<" d[i]:"<<d[i]<<endl;
if (d[i]>mx)
{
mx = d[i];
lst = i ;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
int u,v,w;
m = 0;
n = 0;
while (scanf("%d%d%d",&u,&v,&w)!=EOF)
{
edge[u].push_back(make_pair(v,w));
edge[v].push_back(make_pair(u,w));
m++;
n = max(n,u);
n = max(n,v);
}
ans = inf;
bfs(1);
ans = 0;
// cout<<"lst:"<<lst<<endl;
bfs(lst);
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}