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基础知识
1.View中获取Control和View: //获取控制器名称: ViewContext.RouteData.Values["controller"].ToString(); / ...
- Quartz:Cron Expressions
原文地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html 注意: 位也可能是7位, ...
- 登录成功,拿到token
历尽波折,终于成功登录并拿到了token: - (LoginResultDto *)login:(NSString *)userName andPassword:(NSString *)passwor ...
- W-数据库基础
数据库系统由三部分组成:数据库(DB).数据库管理系统(DBMS)和数据库应用系统 数据加是用来存储数据的,里面存储两大类数据:用户数据及系统数据/数据字典,具体为系统中的用户以及用户孤权限,各种统计 ...
- 计算G711语音的打包长度和RTP里timestamp(时间戳)的增长量
转自:http://blog.csdn.net/xujianglun/article/details/48342367 如何计算G711语音等的打包长度和RTP里timestamp的增长量 一般对于不 ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- 学习linux内核时常碰到的汇编指令(2)
转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvh.html JNGE∶指令助记符——(有符号数比较)不大于且不等于转移(等价于JL).当SF和OF异 ...
- 【项目经验】navicat工具 SQLServer数据库迁移MySQL
新近领了一个任务,就是把SQL Server的数据库迁移到My Sql上,经过查资料,圆满完成任务.分享一下流程. 1.首先,在自己的My Sql数据库上新建一个数据库. 2.打开新建的My Sql数 ...
- Streaming data from Oracle using Oracle GoldenGate and Kafka Connect
This is a guest blog from Robin Moffatt. Robin Moffatt is Head of R&D (Europe) at Rittman Mead, ...
- 【转】saiku与kylin整合备忘录
http://blog.csdn.net/freefishly/article/details/51759133 为什么要整合? Kylin是通过离线预计算将Hive中事实表的各组合维度的值存储在Hb ...