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. 什么是DMI,SMBIOS,符合SMBIOS规范的计算机的系统信息获取方法

    转自:http://www.cnblogs.com/gunl/archive/2011/08/08/2130719.html DMI是英文单词Desktop Management Interface的 ...

  2. Delphi开发中各种文件扩展名代表什么文件

    暂时就遇到了以下这几种,以后遇到再进行补充 .DPR Delphi Project文件,打开这个文件,就会打开所有的编程的代码文件.包含了Pascal代码 .PAS Pascal文件,Pascal单元 ...

  3. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. HTML页面实现返回顶部效果 go to top

    1.首先导入jQuery插件. 2.js代码: $(window).scroll(function () { if($(window).scrollTop()>=100) { $(". ...

  5. wp8 入门到精通 ImageCompress 图片压缩

    //实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelW ...

  6. js中ascii码的转换

    今天在把原来用C写的程序移植到javascript上,但是有个地方一直调不通,后来才发现是js奇葩的字符处理出的问题.c中使用的字符处理比如加上一个字符值强制转换一下,在js中就行不通了. 但是js提 ...

  7. loj 1032 数位dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...

  8. Java学习笔记(九)——继承

    一.继承 1.概念: 继承是类于类之间的关系,是一种"is a "的关系 Ps: Java是单继承 2.优势: (1)子类直接拥有父类的所有属性和方法(除了privata) (2) ...

  9. Jmeter 小攻略(转)

    http://www.myexception.cn/open-source/1346307.html

  10. CentOS配置本地yum源(使用镜像iso文件)

    本人在使用yum安装软件的时候,感觉最不爽的是网络不佳时,安装的速度特别慢.所以,个人就上网search了一下如何使用Linux的安装文件作为其yum源.经过几次尝试,已经可以成功的配置了.下面是详细 ...