Roads in the North POJ - 2631

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+;
const int INF=0x3f3f3f3f; struct Edge{
int v,l,next;
}edge[maxn<<];
int vis[maxn],d[maxn],head[maxn],k,node,ans; void init(){
k = ;
memset(head,-,sizeof head);
} void addedge(int u,int v,int l){
edge[k].v = v;
edge[k].l = l;
edge[k].next = head[u];
head[u] = k++; edge[k].v = u;
edge[k].l = l;
edge[k].next = head[v];
head[v] = k++;
}
void dfs(int u,int t){
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(vis[v] == )
{
vis[v] = ;
d[v] = t + edge[i].l;
if(d[v] > ans)
{
ans = d[v];
node = v;
}
dfs(v,d[v]);
}
}
}
int main()
{
init();
int l,r,len;
while(scanf("%d%d%d",&l,&r,&len) == )
addedge(l,r,len);
memset(vis,,sizeof vis);
vis[] = ;
ans = ;
dfs(,); memset(vis,,sizeof vis);
vis[node] = ;
ans = ;
dfs(node,); printf("%d\n",ans);
};

Roads in the North POJ - 2631的更多相关文章

  1. poj 2631 Roads in the North

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

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

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

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

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

  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(求树的直径,两次遍历 or 树DP)

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

  6. Roads in the North(POJ 2631 DFS)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  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. POJ 2631 Roads in the North (模板题)(树的直径)

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

随机推荐

  1. SpringBoot | 第十二章:RabbitMQ的集成和使用

    前言 上节讲了缓存数据库redis的使用,在实际工作中,一般上在系统或者应用间通信或者进行异步通知(登录后发送短信或者邮件等)时,都会使用消息队列进行解决此业务场景的解耦问题.这章节讲解下消息队列Ra ...

  2. JavaSE_5_线程

    1.多线程中的i++线程安全吗?为什么? 不安全,因为每个线程都有自己的工作内存,每个线程需要对共享变量操作时必须把共享变量从主内存中加载到自己的工作内存,等完成操作后再保存到内存中,如果一个线程运算 ...

  3. subline 安装 package control 连接服务器失败,解决办法

    解决办法: 打开C:\Windows\system32\drivers\etc\hosts文件 增加 50.116.34.243 sublime.wbond.net50.116.34.243 pack ...

  4. leetcode85 Maximal Rectangle

    思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...

  5. CSS单词换行and断词,你真的完全了解吗

    背景 某天老板在群里反馈,英文单词为什么被截断了? 很显然,这是我们前端的锅,自行背锅.这个问题太简单了,css里加两行属性,分分钟搞定. 开心的提交代码,刷新页面.我擦,怎么还是没有断词?不可能啊! ...

  6. angular 学习笔记(3) ng-repeat遍历json并且给样式

    视图: <div ng-app="myapp" ng-controller="myctrl"> <ul> <li ng-repea ...

  7. 用JavaScript访问SAP云平台上的服务遇到跨域问题该怎么办

    关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了.国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了. 直入主题,最近我正在做一个原型开发:通过SA ...

  8. js基础笔录

    1.页面中获取对象 document.getElementById("demo") 2.在页面加载时向 HTML 的 <body> 写文本 document.write ...

  9. xtrabackup支持的engine

    xtrabackup支持的engine 1.InnoDB/XtraDB Hot backup   2.MyISAM with read-lock   3.Archive,CSV with read-l ...

  10. World Wind Java开发之一(转)

    http://blog.csdn.net/giser_whu/article/details/40477235 参照<World wind Java三维地理信息系统开发指南随书光盘>以及官 ...