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<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define Inf 0x3f3f3f3f const int maxn=1e4+;
typedef long long ll;
using namespace std;
struct edge
{
int to,cost;
}; vector<edge> e[maxn];
int farthest,ans;
void dfs(int x,int pre,int dis)
{
for(int i=;i<e[x].size();i++)
{
int xx = e[x][i].to;
if(xx == pre)
continue;
dfs(xx,x,dis+e[x][i].cost);
}
if(dis > ans)
{
ans = dis;
farthest = x;
}
}
int main()
{
int i,j; int x,y;
edge t;
while(scanf("%d%d%d",&x,&y,&t.cost)!=EOF)
{
t.to = y;
e[x].push_back(t);
t.to = x;
e[y].push_back(t);
}
ans = ;
dfs(,-,); dfs(farthest,-,);
printf("%d\n",ans); return ;
}

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

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

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

  2. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

  3. poj--2631--Roads in the North(树的直径 裸模板)

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

  4. Codeforces 1413F - Roads and Ramen(树的直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...

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

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

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

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

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

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

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

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

  10. CF835F Roads in the Kingdom/UOJ126 NOI2013 快餐店 树的直径

    传送门--CF 传送门--UOJ 题目要求基环树删掉环上的一条边得到的树的直径的最小值. 如果直接考虑删哪条边最优似乎不太可做,于是考虑另一种想法:枚举删掉的边并快速地求出当前的直径. 对于环上的点, ...

随机推荐

  1. python8.1多线程

    import threadingimport time def run1 (name,sex): print(name,sex,"执行线程1") time.sleep(3)def ...

  2. Hexo小技巧(包括如何插入本地图片)

    我在研究如何在Hexo中引用本地图片时,看到官方文档对此问题已给出了解决方法,并亲测有效.当然,我并不满足于仅仅知道这一个技巧.在大致阅读过官方文档后,我总结了之前我个人并不知道的几个关于Hexo写博 ...

  3. CSS基础知识(上)

    1.创建结构化.语义丰富HTML 语义化标记是优秀HTML文档的基础. 语义化标记意味着在正确的地方使用正确的元素,从而得到有意义的文档. 有意义的文档可以确保尽可能多的人都能够使用. 1.1 ID和 ...

  4. 9、Bridge 桥梁模式 将类的功能层次结构与实现层结构分离 结构型设计模式

    1.何为桥接模式 桥接模式是一种将类的功能层次和实现层次分离的技术,所谓类的功能层次指的是类要实现什么功能,要定义多少个函数还进行处理,在功能之中我们会用到继承来定义新的方法同时也能使用父类的方法,这 ...

  5. cudnn加速计算

    cudnn加速运算 torch.backends.cudnn.enabled = True torch.backends.cudnn.benchmark = True 第一句话是说,使用的是非确定性算 ...

  6. Cenos系统防火墙开放指定端口

    1.查看已经开放的端口 firewall-cmd --list-ports 2.开启指定端口 firewall-cmd --zone=public --add-port=2181/tcp --perm ...

  7. 2020-03-25:快排、堆排和归并都是O(nlog n)的算法,为何JDK选择快速排序?

    福哥答案2020-03-26: 口诀如下:冒选插希快 堆归计桶基(冒泡,选择,插入,希尔,快速,堆,归并,计数,桶,基数)冒线 平平 稳常小选平 平平 不常小插线 平平 稳常序希线 四组 不常组快四 ...

  8. 2020-03-27:分布式锁的问题,假如a线程在获得锁的情况下 网络波动 极端情况是断网了,这种情况是怎么处理的

    福哥答案2020-04-04:超时释放锁.

  9. Flask实现RESTful API(注意参数位置解析)

    准备工作 首先安装flask_restful三方组件 pip install flask_restful 在models.py中新建一个类,生成表,往里面插入一些数据.(flask要想使用ORM的话需 ...

  10. 获取网页js代码的一个方法

    这个是看了别人的代码,稍加修改而成的.怕时间长忘了,在这里记一笔: console.log(require(["foo:bar.js"]).prototype.someMethod ...