ACM: 强化训练-Roads in the North-BFS-树的直径裸题

时间:2022-05-15 05:09:31
Roads in the North

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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

/*
题意:
有10000个村庄,有很多条路,现在这些路已经把村庄都连了起来,求最远的两个村庄的路的距离。 思路,把每一边都历遍一下,找到两个距离最远的村庄。 这里有一个结论,在图中,要找到距离最远的两点,先随便从一个点入手BFS,找到距离这个点最远的点,在从这个点BFS找到距离这点最远的点,这两点之间的距离就是这棵树的直径。所以直接进行BFS搜索就行了。
*/
#include"iostream"
#include"cstring"
#include"cstdio"
#include"string"
#include"queue"
#include"cmath"
using namespace std;
#define MX 1000000+5
#define memset(x,y) memset(x,y,sizeof(x)) int tt;//记录以某点为起点的所有结果总数 struct Side {
int next,head,cost; //有向边的起点,终点,长度
Side(int n,int h,int c):next(n),head(h),cost(c){};
Side(){};
}; Side side[MX]; //保存所有的有向边
int head[MX]; //起点节点 void AddSide(int u,int v,int cost) {//分别以两个节点为起点记录下起点的坐标和将要搜索的有向边
side[tt]=Side(u,head[v],cost);
head[v]=tt++;
side[tt]=Side(v,head[u],cost);
head[u]=tt++;
} int lenth[MX]; //以某各节点为起点的最长长度 int BFS(int s) {
int maxx=0;//【初始化。。。】
int hed=s;
queue<int>Q;
memset(lenth,-1);
lenth[s]=0;
Q.push(s);
while(!Q.empty()) {
int a=Q.front();
Q.pop();
if(lenth[a]>maxx){//维护最长的距离
maxx=lenth[a];
hed=a; //维护最长距离的起点/头结点
}
for(int i=head[a];~i;i=side[i].head){ //向这条有向边的头结点去找最远的头结点
Side HD=side[i]; //标记头结点
if(lenth[HD.next]==-1){//如果该头结点没有搜索过
lenth[HD.next]=lenth[a]+HD.cost;//lenth记录下之前的最长长度+该有向边长度
Q.push(HD.next); //继续向下搜索。
}
}
}
return hed; //返回距离起点最远的头节点
} int main() {
int u,v,cost;
memset(head,-1);
tt=0;
while(~scanf("%d%d%d",&u,&v,&cost)){
// if(!u&&!v&&!cost)break; //【测试。这题居然没有结束标志。。。。】
AddSide(u,v,cost); //把给出的点构建两条有向边
}
printf("%d\n",lenth[BFS(BFS(1))]); //先从任意一点搜索到距离这个点最远的节点,再反向搜索最远的点就是直径。
//【网上的结论。】
return 0;
}