LCA - 求任意两点间的距离
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.OutputFor 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 题目分析 : 给出一些点之间的长度,再给出一些询问,查任意两点间的距离。
思路分析 : 倍增LCA裸题
代码示例 :
#define ll long long
const int maxn = 4e4+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, m, N;
struct node
{
int to, cost;
node(int _to = 0, int _cost = 0):to(_to),cost(_cost){}
};
vector<node>ve[maxn];
int dep[maxn];
int grand[maxn][30], gw[maxn][30]; void dfs(int x, int fa){
for(int i = 1; i <= N; i++){
grand[x][i] = grand[grand[x][i-1]][i-1];
gw[x][i] = gw[x][i-1] + gw[grand[x][i-1]][i-1];
}
for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
int cost = ve[x][i].cost; if (to == fa) continue;
dep[to] = dep[x] + 1;
grand[to][0] = x;
gw[to][0] = cost;
dfs(to, x);
}
} int lca(int a, int b){
// a 是在 b 的上面的
if (dep[a] > dep[b]) swap(a, b);
int ans = 0; for(int i = N; i >= 0; i--){
if (dep[a] < dep[b] && dep[grand[b][i]] >= dep[a]){
ans += gw[b][i];
b = grand[b][i];
}
}
// a, b 在同一层后
for(int i = N; i >= 0; i--){
if (grand[a][i] != grand[b][i]) {
ans += gw[a][i];
ans += gw[b][i];
a = grand[a][i], b = grand[b][i];
}
}
if (a != b) {
ans += gw[a][0];
ans += gw[b][0];
}
return ans;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t;
int a, b, c; cin >> t;
while(t--){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) ve[i].clear();
for(int i = 1; i < n; i++){
scanf("%d%d%d", &a, &b, &c);
ve[a].push_back(node(b, c));
ve[b].push_back(node(a, c));
}
memset(grand, 0, sizeof(grand));
memset(gw, 0, sizeof(gw));
dep[1] = 0;
N = floor(log(n)/log(2)); // 求出最大的2^k = N中的 k
dfs(1, 1);
for(int i = 1; i <= m; i++){
scanf("%d%d", &a, &b);
printf("%d\n", lca(a, b));
}
}
return 0;
} /*
5
8 100
1 2 1
2 4 1
2 5 1
2 6 1
1 3 1
3 7 1
1 8 1
*/
LCA - 求任意两点间的距离的更多相关文章
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
- HDU2586(LCA应用:在带权树中求任意两点之间的距离)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- Floyed-Warshall算法(求任意两点间最短距离)
思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...
- 计算GPS两点间的距离[单位为:米]
/** * 计算GPS两点间的距离[单位为:米] * @param center GPS当前数据(LonLat对象表示,LonLat.lon表示经度,LonLat.lat表示纬度) ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- 图算法之Floyd-Warshall 算法-- 任意两点间最小距离
1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...
- HDOJ2001计算两点间的距离
计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDOJ/HDU 2547 无剑无我(两点间的距离)
Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...
随机推荐
- CentOS7 添加FTP用户并设置权限
step 1 安装配置Vsftp服务器 一.配置防火墙,开启FTP服务器需要的端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewal ...
- [转]C#操作Memcached帮助类
在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...
- SpringBoot项目改变图片临时文件的存储路径
springboot项目,部署到服务器后,运行一段时间后,处理文件上传的接口时,后报异常. Could not parse multipart servlet request; nested exce ...
- vue-learning:21 - js - mixins
mixins 混入是一种对重复代码的组织方式,可以在多个组件间复用代码. 如果在项目中,在多个组件间有一段逻辑代码是共同的.那常见的处理方式是: 每个组件都复制粘贴代码(显然这是最不好方式) 将以共同 ...
- vue-learning:24 - component - 目录
component 组件 组件的概念 Vue 组件同时也都是 Vue 实例,可接受相同的选项对象option (除了一些根级特有的选项) 和使用相同的生命周期钩子,以及模板调用方式. 组件的构建和注册 ...
- CF1088F Ehab and a weird weight formula
CF1088F Ehab and a weird weight formula 推性质猜结论题 第一步转化,考虑把点的贡献加到边里: $con=\sum (log_2(dis(a_u,a_b))\ti ...
- C++ 图片格式转化和压缩
在做人脸识别底库图片导入的时候,需要支持主流的图片的格式,如jpeg.bmp.png等格式.所以需要对图片进行格式转化.图片过大的话,还有进行缩放等.本文介绍的是利用cximage开源库,来进行对图片 ...
- 17.python内置函数2
python内置函数1:https://www.cnblogs.com/raitorei/p/11813694.html # max,min高级玩法 # l=[1,3,100,-1,2] # prin ...
- ELK学习实验008:Kibana的介绍
一 简介 Kiana是一款开源的数据分析和可视化平台,它是 Elastic Stack成员之一,设计用于和 Elasticsearch协作.您可以使用 Kiana对 Elasticsearch索引中的 ...
- Visual Studio 2017 安装心得
既然VS2017已经发布了,就想安装一下试试,先卸载VS2015, 网上有个完全卸载的东东,https://github.com/Microsoft/VisualStudioUninstaller/r ...