poj 2631 Roads in the North

题目连接

http://poj.org/problem?id=2631 

Roads in the North

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

树的直径。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 11000;
const int INF = 0x3f3f3f3f;
struct edge { int to, w, next; }G[N << 1];
int tot, head[N], dist[N];
void init() {
    tot = 0, cls(head, -1);
}
void add_edge(int u, int v, int w) {
    G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;
    G[tot] = (edge){ u, w, head[v] }; head[v] = tot++;
}
int bfs(int u) {
    queue<int> q;
    q.push(u);
    cls(dist, -1);
    int id = 0, maxd = 0;
    dist[u] = 0;
    while(!q.empty()) {
        u = q.front(); q.pop();
        if(dist[u] > maxd) {
            maxd = dist[id = u];
        }
        for(int i = head[u]; ~i; i = G[i].next) {
            edge &e = G[i];
            if(-1 == dist[e.to]) {
                dist[e.to] = dist[u] + e.w;
                q.push(e.to);
            }
        }
    }
    return id;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    init();
    int u, v, w;
    while(~scanf("%d %d %d", &u, &v, &w)) {
        add_edge(u, v, w);
    }
    printf("%d\n", dist[bfs(bfs(1))]);
    return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4773395.html

5分钟快速验证lvgl界面:VScode模拟器极简配置法(免环境变量版) 本文介绍了一种5分钟快速验证LVGL界面的极简配置方法,使用VSCode模拟器免环境变量配置,适合UI设计师和产品经理快速预览界面效果。通过预编译的SDL2库和配置好的VSCode工程模板,实现开箱即用的LVGL界面验证,大幅提升工作效率。 阅读详情

相关推荐

Ubuntu中snap简单介绍

通过Snap可以安装众多的软件包。需要注意的是,snap是一种全新的软件包管理方式,它类似一个容器拥有一个应用程序所有的文件和库,各个应用程序之间完全独立。所以使用snap包的好处就是它解决了应用程序之间的依赖问题,使应用程序之间更容易管理。但是由此带来的问题就是它占用更多的磁盘空间 介绍一些常用命令,有点类似docker 1、查看已经通过snap安装的snap包 snap list可以查看 2、可以搜索想要的snap包,例如tomcat snap search tomcat

icanflying的博客 4万+

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

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

小张的专栏 793

74hc595驱动模块的Verilog设计(线性状态机)

module hc595_driver( input clk, //50Mhz input rst_n, //复位信号 input [15:0] data, //数码管待显示数据,8位段选数据,8位位选数据 input en, //使能 o...

qq_41294540的博客 3740

poj2631(树的直径)

解题思路:具体做法就是先随便选一个点,进行bfs或dfs,然后距离这个点最远的点,一定是树直径中的一个端点。然后再从这个端点bfs或dfs求得直径。 证明详见https://www.cnblogs.com/a-clown/p/6131346.html #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;vector&gt; #...

qq_39861441的博客 311

TK抓包协议分析:So层逆向

第三个参数是一个回调函数,这里他的返回值1表示成功,0表示失败,那我们手动把他Hook为0,让他不支持quic协议,实现降级。等,我们是为了解决抓包问题,那很有可能是在net目录下(network),那来搜索一下。开了抓包以后APP直接是网络无连接了,并且抓到的请求也是不正常的,下面我们就开始定位。看了半天感觉也没啥有用的东西,那下面我们整理一下已经有信息,来猜测一下,后续怎么进行。那可以猜测,我们真正需要Hook的那个函数也有可能是这样加载的,可以字符串搜索一下。

2503_90751938的博客 3417

【MMDetection3.0实战】从零构建自定义数据集训练流程

本文详细介绍了使用MMDetection3.0框架从零开始构建自定义目标检测数据集的完整训练流程。内容涵盖环境配置、COCO格式数据准备、核心代码改造、模型配置与训练策略调优,以及最终的模型测试与验证,帮助开发者快速上手并训练自己的检测模型。

l6m7n8的博客 313

poj 2631 (求树上的最大距离 基础题)

题目链接:http://poj.org/problem?id=2631 求树上的最大距离 方法

乐观之奋斗者 798

初识数据结构之入门必懂——线性表(顺序表)

本文将介绍数据结构中的线性表与线性表中最简单的结构——顺序表

m0_64705485的博客 905

[树上DP] POJ2631 树的最长路径(最远点对)

题目 对于一棵n个结点的无根树,找到一条最长路径。换句话说,要找到两个点,使得它们的距离最远。 POJ2631 思路 和树的重心问题一样,先把无根树转成有根树。对于任意结点i,经过i的最长路就连接i的两棵不同子树u和v的最深叶子的路径。 基本的求法是,先随便找一个点作为根结点转换为无根树后,遍历每一个点,找出当i为根结点时的子树到叶子的最大距离d(j),在根据d(j)求出结点i作...

OI - IceCab 3242

RoboMaster机甲大师赛备赛:如何用STM32+CAN总线精准控制四个大疆3508电机?

本文详细介绍了在RoboMaster机甲大师赛中,如何利用STM32和CAN总线技术精准控制四台大疆3508电机。从硬件架构设计、多电机ID分配、数据解析到同步控制算法,全面解析了构建高响应、低延迟底盘驱动系统的关键技术,助力参赛队伍提升机器人运动性能。

weixin_28684825的博客 133

调整开关频率处理传导骚扰超标的系统方法

针对某产品在传导骚扰测试中660kHz频点超标问题,分析发现与DC-DC模块开关频率直接相关。通过调整模块开关频率至500kHz,使其避开标准限值严苛频段(0.53-1.8MHz),在不改变硬件设计的情况下实现测试通过。该案例表明,利用EMC标准的频率特性进行"频谱设计"比传统滤波手段更高效,建议在多模块系统中采用1:2:4等频率规划策略,从源头降低噪声耦合风险。这一频率迁移思路为EMC优化提供了新范式。

硬核科技 328

Roads in the North (树形DP)

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 do...

slass-mot-odet 500

使用 Python 构建 Reddit 热帖爬虫:抓取热门讨论帖和评论

在现代互联网中,Reddit 被誉为全球最大、最活跃的讨论平台之一,汇聚了各种主题的帖子、评论和社交互动。因此,抓取 Reddit 上的热门帖子和评论可以为我们提供一个丰富的文本数据源,可以用于情感分析、话题建模、趋势分析等应用。Reddit 提供了官方的 API,可以让开发者获取包括帖子、评论、用户信息等在内的各种数据。我们可以结合上述方法,抓取多个热门帖子,并将每个帖子的标题、作者、发布时间和评论内容保存到一个数据结构中。每个帖子的对象包含了丰富的信息,如标题、内容、发布时间、作者等。

2201_76125261的博客 382

树的直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North

1. 树上面求最长路简单路(无环). 就是树的直径问题. 2. 树的直径问题经典解法:两遍BFS                          (1). 一开始任取一个点u进行搜索查找出距离点u最远距离的点v和长度.                          (2). 第二次BFS则从第一次中的点v找出距离点v最远距离的点的路径长度.                3. 问题正

whyorwhnt的专栏 1166

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【POJ 2631】Roads in the North

1.题目链接。求树的直径裸题,树的直径定义为树上距离最远的两个点。至于求法,bfs,dfs,树DP都可以。简单说一下如何bfs,因为我觉得这个最好实现。(但是据说没办法处理负边权)。其实bfs很简答,就是先随机找一个点,然后找出距离这个点最远的点,再从这个点bfs找出距离这个点最远的点,就是直径。为什么??我举个例子,在一张图像中,是一个人,你想知道这个人的身高,那么你可以现在图上任选一个点,找到...

YG爱木木 290

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Roads in the North POJ - 2631

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 fro...

weixin_30301449的博客 115

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【POJ 2631】 Roads in the North

Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5802 Accepted: 2731 Description Building and maintaining roads among communities in t...

weixin_30734435的博客 120

poj 2631: Roads in the North (树的直径)

题目来源:http://poj.org/problem?id=2631Roads in the NorthTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 3351 Accepted: 1655 DescriptionBuilding and maintaining roads among communitiesin...

da_kao_la的博客 365

POJ 2631 Roads in the North 【树的直径】

题意:输入是一系列行,每行包含三个正整数:一个村的数量,一个不同的村庄的数量,以及连接村庄的路段长度,以公里为单位。 所有路段都是双向的。 您要输出一个整数:该地区最偏远的两个村庄之间的道路距离。 现在可以直接裸树的直径的模板了。

AC_Dreameng 1025

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

2631.Roads in the North 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...

紫藤&code的博客 224

【POJ】2631 - Roads in the North(树的直径)

点击打开题目 Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2526   Accepted: 1241 Description Building and maintaining roads among com

wyg1997 826

POJ2631 Roads in the North

题面:http://poj.org/problem?id=2631 本题是求树的直径裸题。 Code: #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm&gt...

weixin_30505043的博客 137

【POJ】[2631]Roads in the North

Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Description Building and maintaining roads among communities in the far North is an expensive business. With this in

BoilTask Talk Show 548

poj2631Roads in the North 树的直径

Roads in the North 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 ...

cloudy_happy的博客 193

B - Roads in the North POJ-2631

B - Roads in the NorthTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatus Description Building and maintaining roads among communities in the far North is an expe

DTL66的博客 442

POJ 2631 Roads in the North

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...

dingjinhui4074的博客 62
上一篇: poj 2485 Highways
下一篇: poj 2887 Big String
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值