hdu 2586 How far away ?

自动驾驶仿真:python和carsim联合仿真案例 carsim内部有许多相关联合仿真的demo,simulink、labview等等都有涉及,这里简单介绍下python和carsim联合仿真的方法,虽然carsim官方有个Steer_Control.py相关的案例,但是感觉比较冗余,这里抽出重点部分和大家交流探讨下。提示:以下是本篇文章正文内容,下面案例可供参考1、这里关于solvers的细节其实都没说,因为里面确实也没什么内容好讲的,本质就是调用carsim.dll,如果你需要更多的函数解析其实可以看vs_api.h,路径在,具体内容如下图所示。 阅读详情


How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15599    Accepted Submission(s): 5924


Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 

Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 

Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

Sample Input
  
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
 

Sample Output
  
10 25 100 100
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3486  2874  2888  3234  2818 


题意:给你一棵树,每条边都有权值,m次询问,每次问任意两点之间的距离。

思路:我是特意来补这道题的。。因为感觉这个算法挺实用的,好像很多时候都要用到。这是倍增LCA的裸题。思维类似RMQ,通过预处理出每个点的第2^n次方个父节点,从而达到logn查询任意两个节点的最近公共祖先。具体写法看代码吧。。。自己手写的留着当模板用。

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#include<vector>
#include<vector>
#define maxn 40005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int M = 20;
struct node{
	int v, value, next;
}p[maxn << 1];
int len, head[maxn], dis[maxn], depth[maxn], n, m, father[maxn][25];
void addedge(int u, int v, int value){
	p[len].v = v;
	p[len].value = value;
	p[len].next = head[u];
	head[u] = len++;
}
void dfs(int x, int fa){
	father[x][0] = fa;
	for (int i = head[x]; ~i; i = p[i].next){
		if (p[i].v == fa)
			continue;
		dis[p[i].v] = dis[x] + p[i].value;
		depth[p[i].v] = depth[x] + 1;
		dfs(p[i].v, x);
	}
}
void presolve(){
	dis[1] = depth[1] = 0;
	dfs(1, 0);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j < M; j++)
			father[i][j] = father[father[i][j - 1]][j - 1];
}
int lca(int x, int y){
	if (depth[x] != depth[y]){
		if (depth[x] > depth[y])
			swap(x, y);
		int distant = depth[y] - depth[x];
		for (int i = 0; i < M; i++){
			if (distant&(1 << i))
				y = father[y][i];
		}
	}
	if (x == y)
		return x;
	for (int i = M; i >= 0; i--){
		if (father[x][i] != father[y][i]){
			x = father[x][i];
			y = father[y][i];
		}
	}
	return father[x][0];
}
int query(int x, int y){
	return dis[x] + dis[y] - (dis[lca(x, y)] << 1);
}
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		len = 0;
		memset(head, -1, sizeof(head));
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n - 1; i++){
			int u, v, value;
			scanf("%d%d%d", &u, &v, &value);
			addedge(u, v, value);
			addedge(v, u, value);
		}
		presolve();
		while (m--){
			int x, y;
			scanf("%d%d", &x, &y);
			printf("%d\n", query(x, y));
		}
	}
}


Android:Google三方库之Adjust集成详细步骤 Android为APP添加google adjust功能 阅读详情

相关推荐

TwinCAT3 实时核异步通信:基于ADS的C++ Server/Client数据交换实战解析

本文深入解析了在TwinCAT3实时核中,如何利用ADS协议实现C++ Server与Client之间的异步数据交换。文章从核心概念入手,详细阐述了实时核、ADS通信及异步机制,并通过实战步骤指导环境搭建、路由配置、Server端服务创建与Client端请求发起,重点剖析了Req/Ind/Res/Con异步回调模型,旨在帮助开发者构建高效、非阻塞的实时工业控制应用。

purple的博客 469

hdu2586 How far away? LCA

hdu2586 #include #include #include #include using namespace std; #define MAX 40000 struct edge{ int v,w; }; vector mp[MAX]; vector query[MAX]; bool flag[MAX]; int pre[MAX],father[MAX],path[MAX]

Time flies 1943

(一)DaVinci Developer AutoSAR工具 如何新建SWC、Component、RTE工程,超级详细步骤,赚到

​ 1,创建SWC,第一步需要创建一个App组件类型,首先点击Object Brower菜单,然后左键Application Component Types 选项,然后右键New Application Component Type,组件类型的作用是来后续实例一个Compoent 2、 Properties 选项卡中Nane中输入Type的名字,注意根据规范命名规则已CtAp开头,其他默认就好 ​

weixin_42602687的博客 3575

hdu2586【How far away ?】

[tarjian离线求lca] hdu2586【How far away ?】 Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13605 Accepted Submission(s): 5101 【Problem Descript

Freddddd 523

QGIS004:【05矢量属性表】-保留字段、删除字段、添加唯一值索引字段、添加字段到属性表、添加自增量字段、添加XY字段到图层、文本型转浮点型、重构字段、重命名字段

摘要:QGIS矢量属性表工具箱包括保留字段、删除字段、添加唯一值索引字段、添加字段到属性表、添加自增量字段、添加XY字段到图层、文本型转浮点型、重构字段、重命名字段等选项,本文介绍各选项的基本操作。

qq_31762031的博客 2312

白皮精读:70页 2025 基于数据空间的金融数据可信流通研究报告【附全文阅读】

金融数据可信流通面临法规、技术与商业模式的挑战。报告分析国内外发展现状,提出基于数据空间的可信流通框架,包含典型场景、技术方案与流通模式。实践案例显示,隐私计算、区块链等技术可提升风控效率与合规性。建议完善数据分类分级、统一技术标准并推动行业试点,以释放数据要素价值,促进数字经济发展。

cdfunlove的博客 848

HDU 2586 How far away ?

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6309    Accepted Submission(s): 2368 Problem Description There are n

凌风 3953

基于知识图谱的智能工作流引擎

随着大数据和人工智能技术的快速发展,企业和组织对数据的依赖程度日益增加。为了更好地管理和利用这些数据,知识图谱逐渐成为一种重要的工具。知识图谱不仅能够以结构化的方式表示复杂的知识,而且能够通过推理和查询提供深层次的信息。然而,如何将知识图谱应用于实际业务场景中,尤其是如何构建智能工作流引擎,仍然是一个具有挑战性的问题。智能工作流引擎是一种能够自动化执行复杂业务流程的系统,它可以通过对业务规则和数据的分析,自动生成和优化工作流,从而提高工作效率和准确性。

AI天才研究院 1870

HDU 2586 How far away?(LCA)

题目链接; HDU 2586 How far away? 题意: 和POJ 1986 Distance Queries一样的,包括数据范围。 分析: 这里建单向边就能过了。。。#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; typede

ramay7 487

Python爬虫教程:抓取纽约时报文章存档

爬虫技术主要用于从网站上抓取并提取数据。HTTP请求:使用requests库发送HTTP请求以获取网页。网页解析:使用或lxml库解析HTML网页。数据存储:将抓取的数据保存为CSV、JSON等格式,或者存储在数据库中。防反爬虫技术:模拟正常的用户请求,避免被反爬虫机制封锁。多线程与异步爬取:提高抓取效率。通过这篇博客,您已经学习了如何使用Python爬取纽约时报的历史文章存档。本文介绍了如何模拟请求头、抓取网页内容、解析HTML并提取数据,最后存储到CSV文件或数据库中。

2201_76125261的博客 593

【YOLO26实战全攻略】08——YOLO26模型导出与部署全攻略:从训练权重到生产环境(含5种格式+INT8量化+实战案例)

本文聚焦YOLO26模型从训练权重到生产环境的全流程部署,针对“训练精度高、部署效果差”的行业痛点,详解其无NMS端到端推理、量化友好架构、全格式导出三大核心优势。内容涵盖ONNX、TensorRT、OpenVINO、TFLite、CoreML五种主流格式的导出原理与实操步骤,深入剖析INT8量化、混合量化等边缘优化技术,并通过智慧园区车辆检测虚拟案例,完整演示从模型准备、格式导出、引擎适配到性能验证的全流程。文中提供可直接复制运行的代码、详细执行结果及避坑指南,兼顾新手入门与进阶读者提升,更好的帮助读者。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 500

HDU 2586 How far away ?题解与分析

HDU 2586 How far away ? 【题目大意】:           一个村子里有n个房子,这n个房子用n-1条路连接起来,接下来有m次询问,每次询问两个房子a,b之间的距离是多少。 【分析】:          这是个求最近公共祖先的问题,用临接链表存下每条边和询问的信息,然后跑一遍Tarjan(算法详解见:http://blog.youkuaiyun.com/csyzcyj/arti

初学者 1405

ABAP Channels 实战:在 ABAP 7.40 中用 WebSocket 打造实时双向通信

本文详细介绍了SAP ABAP 7.40引入的ABAP Channels通信框架,包含AMC(ABAP Messaging Channels)和APC(ABAP Push Channels)两部分。AMC作为应用服务器内部的消息总线,支持发布/订阅模式,可实现跨会话实时通信;APC基于WebSocket协议,用于浏览器等客户端与ABAP服务器的双向实时交互。文章通过一个乒乓球游戏案例展示了ABAP Channels的实时协作能力,并详细解析了AMC的配置方法、消息发送与接收的代码实现,帮助开发者理解如何利用

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 220

hdu 2586 How far away

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11699    Accepted Submission(s): 4300 Problem Description There are n ho

keshuqi的博客 394

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

HDU 2586 How far away ? LCA

一、内容 There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it h...

Tizzi的博客 914

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU2586 How far away ?【LCA+并查集】

How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 33479 Accepted Submission(s): 13542 Problem Description There are n houses in...

海岛Blog 636

HDU-2586 How far away ?(Tarjian_LCA)

HDU-258 How far away ? 题目 给一颗树,及其每条边权值,求多组两点最短路径, 分析 树上两点最短路径可以先把每个节点到跟节点的权值dis求出,设rt为u,v的LCA。 u,v最短路径 : dis[u]+dis[v]−2∗dis[rt]dis[u] + dis[v] - 2 * dis[rt]dis[u]+dis[v]−2∗dis[rt] 我用的离线Tarjian算法,所以要先...

Stormjing的博客 332

HDU 2586——How far away ?

Time limit 1000 ms Memory limit 32768 kB Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far...

天际苍穹的博客 226

hdu 2586 How far away ?倍增LCA

hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码:...

ddqb3235的博客 234

HDU2586 How far away? —— 倍增LCA

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16425    Accepted

A man can be destroyed, but not defeated. 471

hdu 2586 How far away ? DFS大水题

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2935    Accepted Submission(s): 1095 Problem Description There are n h

hnust_xiehonghao的专栏 2758

HDU 2586. How far away ?

链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意 多次询问,求树形图上两点间距离 思路一 树链剖分 将一个点与其拥有子树节点数目的节点相连,形成一条重链,一颗树最多有 lognlognlogn 条重链 当 xxx 和 yyy 不在一条重链上时,将所在链顶点深度大的那个点往上跳,直到在一条链上 时间复杂度:O((n+m)logn)O((n+m)logn)O((n+m)logn) 代码 #include<bits/stdc++.h> using

c4Lnn 的 优快云 博客 192

How far away ? HDU - 2586

How far away ? Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27977Accepted Submission(s): 11218 Problem Description There are n houses ...

weixin_30902675的博客 240

HDU 2586 How far away?

HDU 2586#pragma comment(linker,"/STACK:1024000000,1024000000") #include&lt;bits/stdc++.h&gt; using namespace std; vector&lt;int&gt;f[40010],flen[40010]; int n,m,dis[40010],dep[40010],fat[40010],p[4001...

Napoleon2004的博客 159

HDU2586(How far away)

How far away 题目传送门 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this “How far is it if I want to go from house A to house B”? Usually it hard to answer. But luck

weixin_43808717的博客 780

HDU2586——How far away ?(LCA模板)

How far away ? 题意:给出一棵树,问两个节点x,y之间的最短距离是多少(边权值)。 思路:用根节点到x的距离+根节点到y的距离−多走的距离根节点到x的距离+根节点到y的距离-多走的距离根节点到x的距离+根节点到y的距离−多走的距离 其中多走的距离要用LCA解决。 LCALCALCALCALCALCALCA求解方法1.欧拉序+++线段树或RMQ欧拉序RMQ(线段树)2.Tarjan LCALCALCA LCALCALCA只是一个概念,它的是一棵树上的两个节点的最近公共祖先。当然,它也是一个节点。

weixin_45363113的博客 607

2586. How far away ?(HDU)

题意理解 一个村庄有很多户人家,任意两户人家最多有一条路相连。已知每条路相连的两户人家的编号和长度,求从A户人家到B户人家有多远? 问题分析 LCA+RMQ数据结构,最近公共祖先+区间最小值 转1:首先用图表示村庄的连接情况。使用邻接表存储,使用heads数组存节点信息,使用edges数组存边信息,edges数组的每个元素存边的长度和邻接边的指针(这里用数组表示链表,所以指针其实是边的下标...

xiexie1357的专栏 336
上一篇: Codeforces Round #411 (Div. 2)E. Ice cream coloring
下一篇: 倍增LCA模板
overload1997
博客等级 码龄11年 328粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值