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. Http中常见MIME类型

    MIME类型 常见MIME类型: 超文本标记语言文本 .html text/html xml文档 .xml text/xml XHTML文档 .xhtml application/xhtml+xml ...

  2. Apache禁止访问网站子目录的方法

    在PHP网站开发中,基于WEB服务器和PHP网站程序代码的安全考虑,我们需要对相关的目录或者文件访问权限进行控制,以防止意外情况的发生,那么我们如何来实现这种功能呢?我们可以通过Apache来实现禁止 ...

  3. 白话SpringCloud | 第二章:服务注册与发现(Eureka)-上

    前言 从本章节开始,正式进入SpringCloud的基础教程.从第一章<什么是SpringCloud>中我们可以知道,一个微服务框架覆盖的东西是很多的,而如何去管理这些服务或者说API接口 ...

  4. SpringBoot | 第七章:过滤器、监听器、拦截器

    前言 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏高词汇.访问权限控制(URL级别)等业务需求.这些对于业务来说一般上是无关的,业务方是无需关系的,业务只需 ...

  5. selenium常用方法,简版介绍

    WebElement 接口共计16个------------接口 代表一个HTML元素.通常,所有与页面交互有关的有趣操作都将通过此界面执行. void clear() void click() We ...

  6. 解决Maven依赖下载不全的问题

    背景描述 在日常学习过程中使用Maven构建SpringBoot+SpringCloud服务时,有时会使用非正式版的SpringBoot和SpringCloud(非正式版是指不是最终发布的版本,而是测 ...

  7. spring运用的设计模式

    1.代理模式(典型的aop) 2.工厂模式(beanFactory) 3.观察者模式(ApplicationContextEvent && ApplicationContextList ...

  8. java.lang.ClassNotFoundException:org/apache/commons/collections/CursorableLinkedList

    明明有 commons-collections.jar 将jar包复制到Tomcat的WEB-INF/lib下就可以了...

  9. 360 so动态脱壳

    环境及工具 手机    : 中兴 U887 系统版本:   Android 2.3.5 工具    :   IDA pro 6.6 .0101Editor 版权声明:未经许可,随便转载 目前so加壳有 ...

  10. Spring MVC中注解的简介

    参考网址:  https://blog.csdn.net/a67474506/article/details/46361195 @RequestMapping映射请求 SpringMVC 使用 @Re ...