POJ 2631 Roads in the North (求树的直径)

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
 1 #include<cstdio>
 2 #include<queue>
 3 #include<string.h>
 4 #define M 100000
 5 using namespace std;
 6 int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
 7 struct stu
 8 {
 9     int from,to,val,next;
10 }st[M];
11 void init()
12 {
13     num=0;
14     memset(head,-1,sizeof(head));
15 }
16 void add_edge(int u,int v,int w)
17 {
18     st[num].from=u;
19     st[num].to=v;
20     st[num].val=w;
21     st[num].next=head[u];
22     head[u]=num++;
23 }
24 void bfs(int fir)
25 {
26     ans=0;
27     int u;
28     memset(sum,0,sizeof(sum));
29     memset(flag,0,sizeof(flag));
30     queue<int>que;
31     que.push(fir);
32     flag[fir]=1;
33     while(!que.empty())
34     {
35
36         u=que.front();
37         que.pop();
38         for(i = head[u] ; i != -1 ; i=st[i].next)
39         {
40             if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
41             {
42                 sum[st[i].to]=sum[u]+st[i].val;
43                 if(ans < sum[st[i].to])
44                 {
45                     ans=sum[st[i].to];
46                     node=st[i].to;
47                 }
48                 flag[st[i].to]=1;
49                 que.push(st[i].to);
50             }
51         }
52     }
53 }
54 int main()
55 {
56     init();
57     while(scanf("%d %d %d",&a,&b,&c)!=EOF)
58     {
59         add_edge(a,b,c);
60         add_edge(b,a,c);
61     }
62
63         bfs(1);
64         bfs(node);
65         printf("%d\n",ans);
66 }
67  //输入后Ctrl+Z输出结果
时间: 2024-10-22 14:54:44

POJ 2631 Roads in the North (求树的直径)的相关文章

poj 2631 Roads in the North

题目连接 http://poj.org/problem?id=2631 Roads in the North 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

POJ 2631 Roads in the North 树的直径

题目大意:裸的树的直径. 思路:随便用一个点跑BFS,求出这个点到所有点的距离,取距离最长的那个点,再用那个点跑BFS,最远的距离就是这棵树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 20010 using namespace std; int x,y,z;

题解报告:poj 2631 Roads in the North(最长链)

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

Roads in the North (树的直径)

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. Give

【poj】 Roads in the North 题解

题目链接:http://poj.org/problem?id=2631 求树的直径模板. 定理: 树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束. 做法: 两边bfs,第一次先找到node(树的直径的两端点其中一个),再一次求node的最长路所结束的点t node->t就是树的直径 #include <queue> #include <cstdio> #include <cstring> #include <iostream> #in

poj:1985:Cow Marathon(求树的直径)

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

poj2631 求树的直径裸题

题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis(u

hdu4612 无向图中任意添加一条边后使桥的数量最少 / 无向图缩点+求树的直径

题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v(level最深)该点必然是树的直径的一个端点,,再从该点出发,bfs,到最深的一点,该点深度就是直径.(证明:先假设u,是直径上一点,S,T是直径的端点,设v!=t,则有(V,U)+(U,S)>(T,U)+(U,S),矛盾,故t=v:若u不是直径上一点,设u到直径上的一点为x,同理易证. 最后 缩

(求树的直径)Warm up -- HDU -- 4612

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之间含有桥的数量最多,然后uv之间的桥都没了,剩下的就是要求的结果: 树的直径的定义刚好就是两个节点之间含有最多的边: 下面是有关树的直径的知识: 这个题目需要手动扩展,不然会爆栈,而且手动扩展的话要用C++提交. 代码: #pragma comment(linker, "/STACK:1024000