博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Roads in the North (树的直径)
阅读量:5010 次
发布时间:2019-06-12

本文共 1708 字,大约阅读时间需要 5 分钟。

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 61 4 56 3 92 6 86 1 7

Sample Output

22 代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x3f3f3f3fconst int maxn=1e4+5;typedef long long ll;using namespace std;struct edge{ int to,cost;};vector
e[maxn];int farthest,ans;void dfs(int x,int pre,int dis){ for(int i=0;i
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 = 0; dfs(1,-1,0); dfs(farthest,-1,0); printf("%d\n",ans); return 0;}

 

转载于:https://www.cnblogs.com/Staceyacm/p/11258299.html

你可能感兴趣的文章
linux修改root账户的用户名所得的教训
查看>>
【LeetCode】Flatten Binary Tree to Linked List
查看>>
读后感-浮生六纪
查看>>
执行指定路径的程序文件
查看>>
Leetcode-950 Reveal Cards In Increasing Order(按递增顺序显示卡牌)
查看>>
[Linux] 在 Linux CLI 使用 ssh-keygen 生成 RSA 密钥
查看>>
14款下载有用脚本的超酷网站
查看>>
LXC-Linux Containers介绍
查看>>
7.31实习培训日志-docker sql
查看>>
c#中使用servicestackredis操作redis
查看>>
ios app 真机crash报告分析
查看>>
CRC标准以及简记式
查看>>
SEO搜索引擎
查看>>
关于本地使用tomcat部署web应用,浏览器自动跳转为https的问题
查看>>
一、Text To Speech
查看>>
Java读取并下载网络文件
查看>>
github上构建自己的个人网站
查看>>
在word中粘贴的图片为什么显示不完整
查看>>
SQL Server 数据库的鼠标操作
查看>>
net软件工程师求职简历
查看>>