poj 2631 Roads in the North (自由树的直径)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4513 | Accepted: 2157 |
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
题目大意:
求树中任意两点最短路长度的最大值。
求树的直径模板题。
还是因为室友的数据结构作业,我才知道有这么个问题。
假设有这么个直径st,s是起点,t是终点。先求任一点到所有其他点的距离,则距离该点最远的点一定在直径上,否则该点就是直径了。再从这个最远的点出发,求该点到所有点的距离,则此次最远的点和上次最远的点必定一个是s,一个是t。
做两次bfs就好了。(树上的单源最短路可以O(n)时间内解决)
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
#define ll long long
#define maxn 10000 int to[maxn*+];
int w[maxn*+];
int next[maxn*+];
int head[maxn+]; struct tnode
{
int point;
int dis;
};
int vis[maxn+]; int main()
{
int cnt=;
memset(head,-,sizeof(vis));
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
to[cnt]=b;w[cnt]=c;next[cnt]=head[a];head[a]=cnt++;
to[cnt]=a;w[cnt]=c;next[cnt]=head[b];head[b]=cnt++;
} std::queue<tnode> q;
memset(vis,,sizeof(vis));
q.push((tnode){,});
vis[]=;
int far=,farp=;
while(!q.empty())
{
tnode node=q.front();q.pop();
if(node.dis>far)
{
far=node.dis;
farp=node.point;
}
for(int i=head[node.point];i!=-;i=next[i])
{
if(!vis[to[i]])
{
q.push((tnode){to[i],node.dis+w[i]});
vis[to[i]]=;
}
}
} memset(vis,,sizeof(vis));
q.push((tnode){farp,});
vis[farp]=;
int ans=;
while(!q.empty())
{
tnode node=q.front();q.pop();
ans=std::max(ans,node.dis);
for(int i=head[node.point];i!=-;i=next[i])
{
if(!vis[to[i]])
{
q.push((tnode){to[i],node.dis+w[i]});
vis[to[i]]=;
}
}
} printf("%d\n",ans); return ;
}
poj 2631 Roads in the North (自由树的直径)的更多相关文章
- 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, 求出每个节点到子树的最长距离和次 ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- C - Roads in the North DFS+树的直径
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
随机推荐
- vim-plug golang定义跳转godef
go get -v github.com/rogpeppe/godef go install -v github.com/rogpeppe/godef ~/.config/nvim/init.vim ...
- Java虚拟机之栈
一.程序计数器(寄存器):PCR 作用:记住下一条JVM指令的执行地址. 特点:①线程私有的 ②不会存在内存溢出 二.虚拟机栈 1.定义 虚拟机栈:线程运行所需要的内存空间. 栈帧:一个栈帧对应一个方 ...
- Component 和 PureComponent 的区别;复制demo,肉眼可以的区别
React.PureComponent它用当前与之前 props 和 state 的浅比较覆写了 shouldComponentUpdate() 的实现.简单来说,就是PureComponent简单实 ...
- Elasticsearch系列---全面了解Document
概要 本篇主要介绍一下document的知识,对document的元数据和基本的语法进行讲解. document核心元数据 前面入门实战一节有简单介绍过document数据示例,这次我们来详细了解一下 ...
- Linux\CentOS MySql 安装与配置
一.MySQL 简介 MySQL 是一个关系型数据库管理系统,是MySQL AB公司开发,现在属于 Oracle 旗下产品. MySQL 采用标准化语言.体积小.速度快.成本低.开源等特点使得一些中小 ...
- Solr搜索引擎【索引提交、事务日志、原子更新】
一.索引提交 当一个文档被添加到Solr中,但没有提交给索引之前,这个文档是无法被搜索的.换句话说,从查询的角度看,文档直到提交之后才是可见的.Solr有两种类型的提交:软提交和正常提交[也称硬提交] ...
- Mybatis日志体系
承接上一篇关于spring 5.x的日志体系,本篇看看Mybatis的日志体系及实现,Mybatis版本基于3.x. 关于mybatis的官方文档比较友好,分门别类,各有论述,如mybatis官方文档 ...
- centos7环境搭建一台mysql服务器启动多个端口
在一台服务器上启动多个mysql实例,分别用不同的端口号,因centos7版本安装mysql5.7后不存在mysqld_multi .mysqld_safe等命令,做踩坑总结 Mysql多实例实现的3 ...
- 前端vue如何下载或者导出word文件和excel文件
前端用vue怎么接收并导出文件 window.location.href = "excel地址" 如果是 get 请求,那直接换成 window.open(url) 就行了 创建一 ...
- [学习笔记] [数据分析] 01.Python入门
1.安装Python与环境配置 ① ② 安装pip以及利用pip安装Python库 2.Anaconda安装 conda list 要在root环境下 3.常用数据分析库 ① Numpy 安装:con ...