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. 一个Nice的生活主题博客模板

    https://www.bitcron.com/ https://api.bitcron.com/ https://chopstack.com/

  2. Codeforces Round #395 (Div. 2) B

    Description Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his ...

  3. Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积

    Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...

  4. RHEL 6.5----heartbeat

    主机名 IP  所需软件  master 192.168.30.130  heartbeat.httpd node-1 192.168.30.131  nfs  node-2 192.168.30.1 ...

  5. RHEL 6.5----CDN(lumanger)

    主机名  IP   服务 master 192.168.30.130  CDN(LuManager) slave 192.168.30.131  DNS  软件安装包下载地址及安装方法 http:// ...

  6. oozie.log报提示:org.apache.oozie.service.ServiceException: E0104错误 An Admin needs to install the sharelib with oozie-setup.sh and issue the 'oozie admin' CLI command to update sharelib

    不多说,直接上干货! 问题详情 关于怎么启动oozie,我这里不多赘述. Oozie的详细启动步骤(CDH版本的3节点集群) 然后,我在查看 [hadoop@bigdatamaster logs]$ ...

  7. 使用预定义的action值启动系统应用

    1.启动浏览器 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); //可以传一个搜索关键字,会直接显示 ...

  8. CF985E Pencils and Boxes

    思路: 先对a数组排序,然后使用动态规划.dp[i]表示前i个能否正确划分.则如果存在dp[j] == 1, i - j + 1 >= k并且a[i] - a[j] < d,那么dp[i] ...

  9. 初学者可能不知道的vue技巧

    前言 大家好,这里是@IT·平头哥联盟,我是首席甩锅官——老金,今天给大家分享的,一些日常中神秘而又简单的vue的实用小技巧,以及我在我司项目中实用vue的总结和坑,跟大家一起分享,希望能给其他攻城狮 ...

  10. 给Sublime Text3 设置自定义快捷键

    Preferrences -> Key Bindings-User打开用户自定义快捷键文件,添加以下代码,保存. [ { "keys": ["ctrl+shift+ ...