Roads in the North
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4513   Accepted: 2157

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

题目大意:

求树中任意两点最短路长度的最大值。

求树的直径模板题。

还是因为室友的数据结构作业,我才知道有这么个问题。

假设有这么个直径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 (自由树的直径)的更多相关文章

  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. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  4. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  5. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  6. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

  7. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  8. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. C - Roads in the North DFS+树的直径

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

随机推荐

  1. 无法优化的O(n!) 算法

    旅行商问题: 有一位旅行商,他需要前往5个城市. 要前往这5个城市,同时要确保旅程最短. 对于每种顺序,他都计算总旅程,再挑选出旅程最短的路线.5个城市有120种不同的排列方式.因此,在涉及5个城市时 ...

  2. 嵌入式、C语言位操作的一些技巧汇总

    下面分享关于位操作的一些笔记: 一.位操作简单介绍 首先,以下是按位运算符: 在嵌入式编程中,常常需要对一些寄存器进行配置,有的情况下需要改变一个字节中的某一位或者几位,但是又不想改变其它位原有的值, ...

  3. Flex实现web版图片查看器

    项目需求: 在web端实现图片浏览,具有放大.缩小.滚轴放大缩小.移动.旋转以及范围控制. 成果图:

  4. Swoft 源码剖析 - Swoole和Swoft的那些事 (Http/Rpc服务篇)

    前言 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一个基于Swoole的框架.Swoole在PHPer圈内学习成本最高的工具 ...

  5. 用Helm3构建多层微服务

    Helm是一款非常流行的k8s包管理工具.以前就一直想用它,但看到它产生的文件比k8s要复杂许多,就一直犹豫,不知道它的好处能不能抵消掉它的复杂度.但如果不用,而是用Kubectl来进行调式真的很麻烦 ...

  6. 23种GoF设计模式的分类

    GoF设计模式一共有23个.一般可以按目的和作用范围来进行划分,具体划分方法如下: 第一,这些模式按目的(即完成什么样任务)来划分为创建型.结构型和行为型这三种模式: 创建型:用来创建对象.单例.原型 ...

  7. python的Requests库的使用

    Requests模块: Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量 ...

  8. 关于servlet报错和jsp中报关于servlet的错误

    servlet-api是对servlet的支持,如果你导入别人的项目后出现servlet中的导包处出现关于javax.servlet.的错误,那么就是缺少这个包了.还有对jsp页面中的报错的支持. 下 ...

  9. Leecode_98_Validate_Binary_Search_Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. python读写配置文件使用总结与避坑指南

    关于今天的内容 最近拿python在写项目部署的相关集成代码,本来两天的工作量,硬是在来回的需求变更中,拖到了一周的时间.今天算是暂时告一段落了.这次由于涉及多个系统的调用和配置参数,代码开发中出现了 ...