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

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

Sample Output

  1. 22
  2.  
  3. /*
    题意:
    有10000个村庄,有很多条路,现在这些路已经把村庄都连了起来,求最远的两个村庄的路的距离。
  4.  
  5. 思路,把每一边都历遍一下,找到两个距离最远的村庄。
  6.  
  7. 这里有一个结论,在图中,要找到距离最远的两点,先随便从一个点入手BFS,找到距离这个点最远的点,在从这个点BFS找到距离这点最远的点,这两点之间的距离就是这棵树的直径。所以直接进行BFS搜索就行了。
    */
  1. #include"iostream"
  2. #include"cstring"
  3. #include"cstdio"
  4. #include"string"
  5. #include"queue"
  6. #include"cmath"
  7. using namespace std;
  8. #define MX 1000000+5
  9. #define memset(x,y) memset(x,y,sizeof(x))
  10.  
  11. int tt;//记录以某点为起点的所有结果总数
  12.  
  13. struct Side {
  14. int next,head,cost; //有向边的起点,终点,长度
  15. Side(int n,int h,int c):next(n),head(h),cost(c){};
  16. Side(){};
  17. };
  18.  
  19. Side side[MX]; //保存所有的有向边
  20. int head[MX]; //起点节点
  21.  
  22. void AddSide(int u,int v,int cost) {//分别以两个节点为起点记录下起点的坐标和将要搜索的有向边
  23. side[tt]=Side(u,head[v],cost);
  24. head[v]=tt++;
  25. side[tt]=Side(v,head[u],cost);
  26. head[u]=tt++;
  27. }
  28.  
  29. int lenth[MX]; //以某各节点为起点的最长长度
  30.  
  31. int BFS(int s) {
  32. int maxx=0;//【初始化。。。】
  33. int hed=s;
  34. queue<int>Q;
  35. memset(lenth,-1);
  36. lenth[s]=0;
  37. Q.push(s);
  38. while(!Q.empty()) {
  39. int a=Q.front();
  40. Q.pop();
  41. if(lenth[a]>maxx){//维护最长的距离
  42. maxx=lenth[a];
  43. hed=a; //维护最长距离的起点/头结点
  44. }
  45. for(int i=head[a];~i;i=side[i].head){ //向这条有向边的头结点去找最远的头结点
  46. Side HD=side[i]; //标记头结点
  47. if(lenth[HD.next]==-1){//如果该头结点没有搜索过
  48. lenth[HD.next]=lenth[a]+HD.cost;//lenth记录下之前的最长长度+该有向边长度
  49. Q.push(HD.next); //继续向下搜索。
  50. }
  51. }
  52. }
  53. return hed; //返回距离起点最远的头节点
  54. }
  55.  
  56. int main() {
  57. int u,v,cost;
  58. memset(head,-1);
  59. tt=0;
  60. while(~scanf("%d%d%d",&u,&v,&cost)){
  61. // if(!u&&!v&&!cost)break; //【测试。这题居然没有结束标志。。。。】
  62. AddSide(u,v,cost); //把给出的点构建两条有向边
  63. }
  64. printf("%d\n",lenth[BFS(BFS(1))]); //先从任意一点搜索到距离这个点最远的节点,再反向搜索最远的点就是直径。
  65. //【网上的结论。】
  66. return 0;
  67. }

  

ACM: 强化训练-Roads in the North-BFS-树的直径裸题的更多相关文章

  1. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

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

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  3. C - Roads in the North DFS+树的直径

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

  4. Roads in the North (树的直径)

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

  5. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  6. poj--2631--Roads in the North(树的直径 裸模板)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2389   Accepted: 117 ...

  7. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  8. POJ 1383 Labyrinth (bfs 树的直径)

    Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...

  9. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

随机推荐

  1. 《C#本质论》读书笔记(12)委托和Lambda表达式

    12.1.委托概述 12.1.2 委托的数据类型 为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法.此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型--也 ...

  2. Linux安装mysql最新版本纪要

    http://blog.csdn.net/frt007/article/details/50184143 http://blog.csdn.net/wb96a1007/article/details/ ...

  3. Oracle Redhat5.5

    http://blog.csdn.net/yakson/article/details/9012129 http://www.cnblogs.com/cnmarkao/p/3670153.html h ...

  4. MYSQL中'TYPE=MyISAM'错误的解决方案

    create 语句后面的TYPE=MyISAM TYPE=MyISAM 和 ENGINE=MyISAM 都是设置数据库存储引擎的语句 ,(老版本的MySQL使用TYPE而不是ENGINE(例如,TYP ...

  5. Snowflake Snow Snowflakes(哈希表的应用)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 27312   Accep ...

  6. <转>ORA-06413 连接未打开错误

    ORA-06413 Connection not open.Cause: Unable to establish connection.Action: Use diagnostic procedure ...

  7. Linux环境下使用perl编写CGI(httpd)

    例子1: /var/www/cgi-bin/hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print ...

  8. 在Xcode中想要清屏该怎么实现

    XCODE的控制台不会有清屏效果,并没有像终端一样可以clear.但在某些时候我们非得想要清屏该怎么办呢??你去打开可执行文件,就会有类似清屏的效果.实际上是它帮你自动换页了,xcode左边是可以看到 ...

  9. 通过PID获取进程路径的几种方法

    通过PID获取进程路径的几种方法 想获得进程可执行文件的路径最常用的方法是通过GetModuleFileNameEx函数获得可执行文件的模块路径这个函数从Windows NT 4.0开始到现在的Vis ...

  10. document对象(二)

    五.相关元素操作: var a = document.getElementById("id");                找到a: var b = a.nextSibling ...