题干:

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area. The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.

Input

Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

Output

You are to output a single integer: the road distance between the two most remote villages in the area.

Sample Input

5 1 6 1 4 5 6 3 9 2 6 8 6 1 7

Sample Output

22

解题:

There are many places where it is easy to be wrong, maxx and rept in bfs can't Assignment, otherwise it will be wa! Think about it too, the limit case, only one point, or an empty picture, in short, can construct a loop that allows him to not enter the loop, or can not enter the while, so need to initialize! This is also why the sentence w=maxx cannot be placed in the comment.

AC Code:

```
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
Using namespace std;
Const int MAX = 2e5 + 5 ;
Const int INF = 0x3f3f3f3f;
Struct Node {
Int to;
Int w;
Int ne;
} e[MAX];
Struct point {
Int pos,c;
Point(){}//No such constructor can't write node t like this
Point(int pos, int c): pos(pos),c(c){}// can write node(pos,cost) like this
};
Int head[MAX];
Int cnt = 0 ;
Bool vis[MAX];
Void init() {
Cnt = 0;
Memset(head,-1,sizeof(head));
}
Void add(int u, int v, int w) {
e[cnt].to = v;
e[cnt].w = w;
e[cnt].ne = head[u];
Head[u] = cnt;
Cnt++;
}
Int bfs(int x,int &w) {
Queue <point> q;
Int maxx = 0;
Int retp = x ;//point coordinates returned
Memset(vis,0,sizeof(vis) );
Q.push(point(x,0));vis[x] = 1;
Point now;
While(q.size() ) {
Point cur = q.front();
Q.pop();
For(int i = head[cur.pos]; i!=-1; i=e[i].ne) {
If(vis[e[i].to]) continue;
Vis[e[i].to] = 1;
Now.pos = e[i].to;
Now.c = cur.c + e[i].w;
If(now.c>maxx) {
Maxx = now.c;
Retp = now.pos;
}
Q.push(now);
}
//w = maxx;
}
w = maxx;
Return retp;
}
Int main()
{
Init();
Int u,v,w;
While(~scanf("%d%d%d",&u,&v,&w) ) {
Add(u,v,w);
Add(v,u,w);
}
Int ans1 = 0, ans2 = 0;
u = bfs(1,ans1);
v = bfs(u,ans2);
Printf("%d\n",ans2);
Return 0 ;
}
```