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
 #include<cstdio>
#include<queue>
#include<string.h>
#define M 100000
using namespace std;
int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
struct stu
{
int from,to,val,next;
}st[M];
void init()
{
num=;
memset(head,-,sizeof(head));
}
void add_edge(int u,int v,int w)
{
st[num].from=u;
st[num].to=v;
st[num].val=w;
st[num].next=head[u];
head[u]=num++;
}
void bfs(int fir)
{
ans=;
int u;
memset(sum,,sizeof(sum));
memset(flag,,sizeof(flag));
queue<int>que;
que.push(fir);
flag[fir]=;
while(!que.empty())
{ u=que.front();
que.pop();
for(i = head[u] ; i != - ; i=st[i].next)
{
if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
{
sum[st[i].to]=sum[u]+st[i].val;
if(ans < sum[st[i].to])
{
ans=sum[st[i].to];
node=st[i].to;
}
flag[st[i].to]=;
que.push(st[i].to);
}
}
}
}
int main()
{
init();
while(scanf("%d %d %d",&a,&b,&c)!=EOF)
{
add_edge(a,b,c);
add_edge(b,a,c);
} bfs();
bfs(node);
printf("%d\n",ans);
}
//输入后Ctrl+Z输出结果

POJ 2631 Roads in the North (求树的直径)的更多相关文章

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

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  2. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  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 (自由树的直径)

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

  5. poj 2631 Roads in the North

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

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

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

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

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

  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. 布线问题 最小生成树 prim + kruskal

    1 : 第一种 prime     首先确定一个点 作为已经确定的集合 , 然后以这个点为中心 , 向没有被收录的点 , 找最短距离( 到已经确定的点 ) , 找一个已知长度的最小长度的 边 加到 s ...

  2. 51Nod 1649 齐头并进

    #include <iostream> #include <algorithm> #include <cstring> //两遍迪杰斯特拉 #define INF ...

  3. 连接php/mysql

    1 安装php5.6 首先确保mysql已安装 ./configure --prefix=/app/php-5.6.36 --with-mysql=mysqlnd --enable-mysqlnd - ...

  4. Spring------自动化装配Bean(二)

    上一篇是基于 @ComponentScan自动装配Bean的实现,这一篇将通过java手动装配bean来实现. 手动装配相对于自动装配的优点: 可以自行定义Bean的各个属性. 添加额外的方法调度. ...

  5. DedeCMS全版本通杀SQL注入漏洞利用代码及工具

    dedecms即织梦(PHP开源网站内容管理系统).织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,近日,网友 ...

  6. 动手实现 Redux(一):优雅地修改共享状态

    从这节起我们开始学习 Redux,一种新型的前端“架构模式”.经常和 React.js 一并提出,你要用 React.js 基本都要伴随着 Redux 和 React.js 结合的库 React-re ...

  7. state vs props

    我们来一个关于 state 和 props 的总结. state 的主要作用是用于组件保存.控制.修改自己的可变状态.state 在组件内部初始化,可以被组件自身修改,而外部不能访问也不能修改.你可以 ...

  8. oracle 数据导入、导出

    导入导出 --数据导出备份和导入 ------注意 导出和导入 必须是CMD 命令行下操作,而不是SQL编辑器中 --1.导出表 . --exp:导出关键字 ,userid:用户权限 ,file:保存 ...

  9. ibatis学习笔记

    步骤: 搭建配置环境:导入相关jar包 配置文件: JDBC连接属性文件 总配置文件 关于每个实体的映射(map.xml)文件 JDBC连接属性文件 jdbc.properties ## mysql ...

  10. Android模板制作

    本文详细介绍模板相关的知识和如何制作Android模版及使用,便于较少不必要的重复性工作.比如我在工作中如果要创建一个新的模块,就不要需要创建MVP相关的几个类:Model.View.Presente ...