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 ...
随机推荐
- 【Luogu P2471】[SCOI2007]降雨量
Luogu P2471 啊啊啊啊这真是一道史上最毒瘤的题目!!!!! 题意就是给出n个年份的降雨量 询问:"自从\(y\)年以来\(x\)年的降雨量最大"的正确性. 显然有多种情况 ...
- 【2018寒假集训Day 1】【位运算】生成字符串
生成字符串(strs) [问题描述] 假设字符串只由字符“0”,“1”,“”组成,其中字符“”表示该字符可由 字符“0”或“1”替代. 现有一些字符串,根据这些字符串生成所有可生成的字符串.如: {1 ...
- postgresql更改配置生效问题
补充:如何确定psql配置文件的路径 ①切换至psql用户,此处为thunisoft. ②确定路径方法很多,此处介绍常用的几种. <1>ps –ef |grep base 输出结果中 – ...
- es6 every的使用
let arr2 =[1,3,5,7,9,10]; //arr2.every() 数组里面所有的元素都有符合条件,才返回true var b =arr2.every(function (val,ind ...
- CSP2019,RP+=150。
csp2019游记 第一题水死了,2分钟过! 第二题被卡住了,才得了30分. 第三题背包DP,可惜了,出了考场才知道... 第四题骗到20分,也算心满意足了.. 150,省二左右,辣鸡... 反正就是 ...
- Logistic回归算法梯度公式的推导
最近学习Logistic回归算法,在网上看了许多博文,笔者觉得这篇文章http://blog.kamidox.com/logistic-regression.html写得最好.但其中有个关键问题没有讲 ...
- Django REST framework(DRF)
Django REST framework(DRF) FBV与CBV CBV源码分析 Restful接口规范 DRF简单配置使用和源码解析 DRF序列化器 DRF10大接口 DRF视图集 三大组件 j ...
- 带着canvas去流浪系列之一:绘制柱状图
[摘要] 学习使用canvasAPI来实现数据可视化. 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制柱 ...
- Git 如何优雅地回退代码
前言 从接触编程就开始使用 Git 进行代码管理,先是自己玩 Github,又在工作中使用 Gitlab,虽然使用时间挺长,可是也只进行一些常用操作,如推拉代码.提交.合并等,更复杂的操作没有使用过, ...
- DRF Django REST framework 之 序列化(三)
Django 原生 serializer (序列化) 导入模块 from django.core.serializers import serialize 获取queryset 对queryset进行 ...