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. vue-cli从2升级到3报错error 404 Not Found: @wry/context@^0.4.0

    vue3出来了,想尝尝鲜. 于是按官方的方法卸载2安装3. npm uninstall vue-cli -g npm install -g @vue/cli 但是报错了 error 404 Not F ...

  2. 解决maven创建过慢问题和快捷键

    archetypeCataloginternal idea常用的快捷键 Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ct ...

  3. vue通过控制boolean值来决定是否添加class类名

    vue通过控制boolean值来决定是否添加class类名

  4. 深度学习解决NLP问题:语义相似度计算

    在NLP领域,语义相似度的计算一直是个难题:搜索场景下query和Doc的语义相似度.feeds场景下Doc和Doc的语义相似度.机器翻译场景下A句子和B句子的语义相似度等等.本文通过介绍DSSM.C ...

  5. Python和Java的区别

    这里是我的一些总结,有些是参考别人的(在这里谢谢!!!) 区别: 1.Python比Java简单,学习成本低,开发效率高2.Java运行效率高于Python,尤其是纯Python开发的程序,效率极低3 ...

  6. golang实现rabbitmq消息队列失败尝试

    在工作中发现,有些时候消息因为某些原因在消费一次后,如果消息失败,这时候不ack,消息就回一直重回队列首部,造成消息拥堵. 如是有了如下思路: 消息进入队列前,header默认有参数 retry_nu ...

  7. 金蝶天燕中间拒绝put、delete请求解决方案

    项目要求支持国产化,那就国产化呗!使用金蝶天燕中间件替代weblogic,一切部署好后发现所有以put.delete请求的按钮全部无效,原因是中间件配置文件默认拒绝put.delete请求 解决方案为 ...

  8. Chapter 02—Creating a dataset(Part2)

    三. 导入数据 图02-03:Source of data that can be imported into a dataset 11. 从键盘导入数据 (1)可能是最简单的数据导入方式. (2)使 ...

  9. Vue项目解析

    各个文件夹 node_modules:用来放环境依赖 public:用来放公共资源,里面的index.html文件,就是初始的挂载点.被app.vue给取代了. src:放各种资源的. assets: ...

  10. (python)查看糗事百科文字 点赞 作者 等级 评论

    import requestsimport reheaders = { 'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; ...