-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ-2631-Roads in the North-easy.txt
86 lines (80 loc) · 1.28 KB
/
POJ-2631-Roads in the North-easy.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
*POj-2631-Roads in the North
*xuchen
*2015-10-30 13:17:02
**/
/**
*diameter of tree
*Watch out! The input data contains null
*in which case must print 0
**/
#include "stdio.h"
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 10005;
struct edge{
int to, w, next;
};
edge edges[N*N];
int heads[N];
int lon;
queue<int> q;
int dis[N];
bool vis[N];
void mkEdge(int from, int to, int w)
{
edges[++lon].to = to;
edges[lon].w = w;
edges[lon].next = heads[from];
heads[from] = lon;
}
void bfs(int node)
{
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
int cnode;
q.push(node);
while(!q.empty())
{
cnode = q.front();
vis[cnode] = true;
q.pop();
for(int d = heads[cnode]; d!=0; d = edges[d].next)
{
if(vis[edges[d].to])
continue;
dis[edges[d].to] = dis[cnode]+edges[d].w;
q.push(edges[d].to);
}
}
}
int main()
{
int from=0, to, w, n=0;
while(scanf("%d%d%d", &from, &to, &w)!=EOF)
{
mkEdge(from, to, w);
mkEdge(to, from, w);
}
int tmpnode=0, tmpmax = 0;
bfs(from);
for(int i=1; i<N; i++)
{
if(tmpmax<dis[i])
{
tmpmax = dis[i];
tmpnode = i;
}
}
bfs(tmpnode);
for(int i=1; i<N; i++)
{
if(tmpmax<dis[i])
tmpmax = dis[i];
}
printf("%d\n", tmpmax);
return 0;
}