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;
}

  

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++ 子类调用父类构造方法 调用父类方法 类声明与实现分离

    Person.h #pragma once #include "stdafx.h" #include<iostream> class Person { private: ...

  2. 解决css样式被内置样式覆盖的问题

    .preImg { height:400px !important } <img id="preImg" class="preImg" style=&qu ...

  3. js onclick="return test()"事件返回值,对有些事件,会影响默认动作的执行。如:onclick和onsubmit

    onclick="return test()"事件返回值,对有些事件,会影响默认动作的执行.如:onclick和onsubmit <body> <!--事件返回值 ...

  4. 6-03使用SQL语句一次型向表中插入多行数据

    通过将现有表中的数据添加到已存在的表中: INSERT INTO <表名><列名> SELECT<列名> FROM<源表名> 将UserInfo的数据添 ...

  5. [Tools] 使用work2013发布博客

    参考园子里推荐的方式,觉得使用word发布挺好的,尝试了一下,还不错,记录下来备用   参考连接: http://www.cnblogs.com/liuxianan/archive/2013/04/1 ...

  6. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了

    转自:http://blog.csdn.net/kiddy19850221/article/details/6655066 uint8_t / uint16_t / uint32_t /uint64_ ...

  7. matlab练习程序(碎片)

    这个算法是对photoshop中滤镜->像素化->碎片这个功能的学习. PS这个功能好像不带参数,不过我这里有滤波半径r可以进行控制. 因为我是看效果猜算法的,效果肯定有所区别. 我的想法 ...

  8. git warning: LF will be replaced by CRLF in...

    如果你有git项目,在提交代码的过程中可能会碰到上面的警告,特别是的项目中包含序列化对象的时候,你可能要小心!! 警告的含义是说换行符的,不同的操作系统的换行符是不一致的,如果你不清楚,真得看看这个 ...

  9. 孙鑫VC学习笔记:多线程编程

    孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modified ...

  10. h5嵌入视频遇到的bug及总结---转载

    最近做的一个h5活动因为嵌入视频而发现了好多以前从未发现的问题,在测试的时候不同系统不同版本不同环境等多多少少都出现了些问题,搞得我也是焦头烂额的,不过好在最终问题都解决了,自己也学到了好多东西,为了 ...