Poj2631--roads in the north (diameter of the tree)

Source: Internet
Author: User

Roads in the north
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2428 Accepted: 1190

Description

Building and maintaining roads among communities in a expensive business. With the roads be build such that there be only one route from a village to a village that does not pass th Rough some other village twice.
Given is an area in the comprising a number of villages and roads among them such so any village can be Reache D by road from any other village. Your job is to find the road distance between the "most remote villages in the" area.

The villages connected by road segments. The villages is numbered from 1.

Input

Input to the problem are 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 is two-way.

Output

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

Sample Input

5 1 61 4 56 3 92 6 86 1 7

Sample Output

22

Source

The UofA Local 1999.10.16 map of the longest road, any selection of BFS to reach the longest one endpoint---> to find the longest road;
#include <queue>#include<cstdio>#include<cstring>#defineN 100000+10#defineM 100000+10using namespacestd;structedge{int  from, to, Val, next;} EDGE[M];intHead[n], CNT;intDist[n];BOOLVis[n];intnode;intans;voidinit () {CNT=0; memset (Head,-1,sizeof(head));}intDfsintSX) {Queue<int>Q; memset (Dist,0,sizeof(Dist)); memset (Vis,false,sizeof(VIS)); VIS[SX]=true;    Q.push (SX); Node=SX; ans=0;  while(!Q.empty ()) {        intu=Q.front ();        Q.pop ();  for(intI=head[u]; i!=-1; I=Edge[i].next) {Edge E=Edge[i]; if(!vis[e.to]&&dist[e.to]<dist[u]+e.val) {dist[e.to]=dist[u]+E.val; Vis[e.to]=true;                Q.push (e.to); if(dist[e.to]>ans) {ans=Dist[e.to]; Node=e.to; }            }        }     }}voidsolve () {DFS (1);    DFS (node); printf ("%d\n", ans);}voidAddintUintVintW) {Edge E={u, V, W, Head[u]}; EDGE[CNT]=E; Head[u]=cnt++;}intMain () {intA, B, C;    Init ();  while(SCANF ("%d%d%d", &a, &b, &c)! =EOF)         {Add (A, B, c);    Add (b, A, c);    } solve (); return 0; } 

Poj2631--roads in the north (diameter of the tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.