ACM: 强化训练-Roads in the North-BFS-树的直径裸题
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
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
Output
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-树的直径裸题的更多相关文章
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- C - Roads in the North DFS+树的直径
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
- Roads in the North (树的直径)
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- poj--2631--Roads in the North(树的直径 裸模板)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2389 Accepted: 117 ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- POJ 1383 Labyrinth (bfs 树的直径)
Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...
- codeforce 337D Book of Evil ----树形DP&bfs&树的直径
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
随机推荐
- MVC学习笔记--跟小静学MVC相关语法特性小补习
http://www.cnblogs.com/janes/archive/2012/10/15/2721101.html http://www.cnblogs.com/h82258652/p/4795 ...
- jQuery学习笔记---兄弟元素、子元素和父元素的获取
我们这里主要总结jQuery中对某元素的兄弟元素.子元素和父元素的获取,原声的Javascript代码对这些元素的获取比较麻烦一些,而jQuery正好对这些方法进行封装,让我们更加方便的对这些元素进行 ...
- HTML5 – 2.新元素
figcaption 定义和用法 <figcaption> 标签定义 figure 元素的标题(caption). "figcaption" 元素应该被置于 " ...
- 几年前做家教写的C教程(之三专讲了递归和斐波那契)
C语言学习宝典(3) 数组: 一维数组的定义: 类型说明符 数组名[常量表达式] 例如: int a[10]; 说明:(1)数组名的命名规则和变量名相同,遵循标示符命名规则 (2)在定义数组时需要 ...
- 限制非安全IP访问
这个是一个检测ip是否非法的php函数,适应于白名单,黑名单功能开发,主要场景应用于:api来源限制,访问限制等. /** * 安全IP检测,支持IP段检测 * @param string $ip 要 ...
- [CentOS]安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决
转自:http://blog.csdn.net/wanglei2258/article/details/24961233 [CentOS]安装软件:/lib/ld-linux.so.2: bad EL ...
- 安装wine的问题
- win10 Edge浏览器一打开就闪退崩溃的解决思路
故障现象:从Win7.Win8.1升级到Win10,或是使用Win10一段时间后,发现Edge浏览器打开一到两秒就闪退,崩溃无法打开.解决方案: 1.尝试清理: C:\Users\Administra ...
- C语言中的位操作(14)--反转比特位
本篇文章主要讲述几种反转比特位的方法: 将一个32位数:abcd efgh 转置为hgfe dcba 1.常规方法 unsigned int v; // 目标待转置数 unsigned int r = ...
- 串口编程 tcflush()函数 (转)
tcflush函数刷清(扔掉)输入缓存(终端驱动法度已接管到,但用户法度尚未读)或输出缓存(用户法度已经写,但尚未发送). int tcflush(int filedes,int quene) qu ...