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

时间:2023-02-19 10:01:30
Roads in the North
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2389   Accepted: 1173

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

Source

The UofA Local 1999.10.16

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 10010
int Node,m,n,cnt,ans;
int head[MAXN],dis[MAXN],vis[MAXN],pre[MAXN];
struct node
{
int u,v,val;
int next;
}edge[MAXN];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
for(int i=0;i<10010;i++)
pre[i]=i;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void bfs(int sx)
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
ans=0;
queue<int>q;
Node=sx;
q.push(Node);
vis[Node]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[u]+E.val;
q.push(E.v);
}
}
}
for(int i=1;i<=cnt;i++)
{
if(ans<dis[i])
{
ans=dis[i];
Node=i;
}
}
}
void slove()
{
bfs(1);
bfs(Node);
printf("%d\n",ans);
}
int main()
{
int a,b,c;
init();
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
add(a,b,c);
add(b,a,c);
}
slove();
return 0;
}

poj--2631--Roads in the North(树的直径 裸模板)的更多相关文章

  1. POJ 2631 Roads in the North&lpar;树的直径&rpar;

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

  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

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

  4. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

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

  5. poj 2631 Roads in the North (*树的直径)

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

  6. POJ 2631 Roads in the North &lpar;模板题&rpar;&lpar;树的直径&rpar;

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

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

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

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

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

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

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

随机推荐

  1. 《第一行代码》学习笔记——第1章 开始启程,你的第一行Android代码

    1.3 创建你的第一个Android项目 1.3.1 创建HelloWorld项目 1.Application Name代表应用名称,手机上显示的就是它: 2.Project Name代表项目名称,其 ...

  2. COM组件(MFC篇)

    目录 第1章创建进程内组件    1 1.1 目标    1 1.2 创建项目    3 1.2.1 VC++6.0    3 1.2.2 VC++2010    4 1.2.3 VC++6.0与VC ...

  3. 解决Windows 7删除执行过的 EXE、Bat文件有延迟的问题

    解决了困扰已久的问题,真是大快人心啊! Win7删除exe文件刷新重现及删除慢问题解决方法 - DragonCheng的专栏 - 博客频道 - CSDN.NET Win7删除exe文件刷新重现及删除慢 ...

  4. 201521145048《Java程序设计》第4周学习总结

    1. 本章学习总结 学会了如何去设计一个类,尽量用private修饰属性,public修饰方法. 了解继承的目的. 了解继承和多态的关系. 了解关键字extends super final overr ...

  5. 用es6方式的写的订阅发布的模式

    //发布订阅模式 class EventEmiter { constructor() { //维护一个对象 this._events = { } } on(eventName, callback) { ...

  6. Bigger-Mai 养成计划,Python基础巩固四

    一.装饰器:定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能.原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰函数的调用方式实现装饰器的知识储备:1.函数即‘变量’2.高阶函数 ...

  7. &lbrack;Chrome&rsqb; 谷歌浏览器开启开发模式仍然无法安装油猴脚本

    右键 > 属性 > 起始位置 > 添加 --enable-easy-off-store-extension-install 谷歌浏览器无法安装油猴脚本:--enable-easy-o ...

  8. Module&lpar;模块&rpar;

    1.每个Angular至少有一个根Module 2.Module时一个带有@NgModule装饰符的类 3.最简单的Module import { NgModule } from '@angular/ ...

  9. NPM install 中:-save 、 -save-dev 和 没有--save的情况

    原文地址:https://www.cnblogs.com/limitcode/p/7906447.html npm install moduleName 命令 . 安装模块到项目node_module ...

  10. T-SQL 之 视图

    视图实际上就是一个存储查询,重点是可以筛选.组合和匹配来自基本表(或者其他视图)的数据,从而创建在很多方面像另一个基表那样起作用的对象.可以创建一个简单的查询,仅仅从一个表中选择几列,而忽略其他列:或 ...