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

题目分析

乍一看,这个是求最短路的问题,本想着prim算法弄完,但是奈何这个题目被归在了RMQ&LCA专题里面,而且这个结点的个数最大有4w个,为了防止用prim算法出现超时,最后还是老老实实的去学了LCA算法,我推荐一个学这个东西的地方,讲的还不错,有这个题目的原题讲解 https://www.bilibili.com/video/av41067872/?p=5

好了,来分析一下这个题目吧,求指定村庄之间的最短距离,因为要用到 Lowest Common Ancestor 算法,我们用dis[x]表示结点(也就是城市x)离根结点的距离,由于题目的特殊性, "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" , 这个地方说明了每两个城市之间的路是固定的,有且只有一条(想到这里,发现哪里是什么最短路,就是要你求两点之间的距离)。

假设u,v为要求距离的两个城市,k表示二者的LCA,那么dis[u] - dis[k] 为u距离LCA的距离,而dis[v] - dis[k] 为v距离LCA的距离,所以dis[u] + dis[v] - 2 * dis[k] 就是两个城市之间的距离,然后其他的步骤就按照LCA算法求出LCA就好,LCA的教程请参考上面的哪个网址,个人觉得讲的很好。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int max2 = 100 + 10;
const int max3 = 1000 + 10;
const int max4 = 10000 + 10;
typedef struct Edge{
int to;
int val;
Edge(int to = 0 , int val = 0):to(to),val(val){}
}Edge; vector<Edge>road[4 * max4]; //road[x]保存从x出发的所有道路,road[x][i].to表示这条路的另一个点
int dis[4 * max4]; //dis[x]记录结点x离根结点的距离
int father[4 * max4][15]; //father[x][j]记录从x点向上2^j次层的祖先结点
int depth[4 * max4]; //depth[x]记录点x的深度
int lg[4 * max4]; //log2n 向下取整 void init()
{
memset(dis, 0, sizeof(dis));
memset(father, 0, sizeof(father));
memset(depth, 0, sizeof(depth));
for (int i = 0; i < 4 * max4;i++)
road[i].clear();
} void dfs(int now,int fa)
{
depth[now] = depth[fa] + 1;
father[now][0] = fa; //将now点的祖先结点全部记录下来
for (int j = 1; j <= lg[depth[now]] + 1; j++)
father[now][j] = father[father[now][j - 1]][j - 1]; //搜索当前结点的子结点
for (int i = 0; i < road[now].size();i++)
{
if(road[now][i].to != fa) //如果某一条路是当前结点和父结点的,那么没有必要向上搜索
{
dis[road[now][i].to] = dis[now] + road[now][i].val; //子结点到根结点的距离等于父结点到根结点的距离加上这条路的距离
dfs(road[now][i].to, now);
}
}
} //返回结点u,v的最小公共祖先
int lca(int u,int v)
{
if (depth[u] < depth[v])
swap(u, v); while (depth[u] != depth[v])
u = father[u][lg[depth[u] - depth[v]]]; if (u == v)
return u; for (int i = lg[depth[u]];i >= 0;i--)
{
//当两者的某祖先结点不一致的时候,说明还需要继续向上查找
//当u,v位lca的最近子结点的时候,对于任意的father[u][i]都满足
//father[u][i] == father[v][i] ,所以最后u,v为lca的最近子结点
if(father[u][i] != father[v][i])
{
u = father[u][i];
v = father[v][i];
}
}
return father[u][0];
} int main()
{
//预处理,lg[x]表示log2n向下取整
lg[0] = -1;
for (int i = 1; i < 4 * max4;i++)
lg[i] = lg[i >> 1] + 1; int t;
scanf("%d",&t);
while(t--)
{
init();
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i < n; i++)
{
int s, e, v;
scanf("%d%d%d", &s, &e, &v);
road[s].push_back({ e,v });
road[e].push_back({ s,v });
}
dfs(1, 0); //树中任意一个结点均可以作为根结点,但是不确定结点的个数,而且编号为1的结点所以的树均有,所以将1作为根结点
while(m--)
{
int s, e;
scanf("%d%d", &s, &e);
int k = lca(s, e);
printf("%d\n", dis[s] + dis[e] - 2 * dis[k]);
}
}
return 0;
}

HDU 2586——How far away ?的更多相关文章

  1. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  2. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  3. HDU 2586 How far away ?【LCA】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...

  4. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  5. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  6. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  7. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  8. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  9. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

  10. hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Unity3D_(物理引擎)Rigidbody组件

    Rigidbody组件刚体(Rigidbody)是Unity物理引擎中的重要组件,一个游戏对象添加了刚体组件以后就开始接受物理力学的影响了,比如重力.空气摩擦力.碰撞产生的推力   Learn 相同高 ...

  2. [CSP-S模拟测试]:爬(贪心)

    题目传送门(内部题134) 输入格式 第一行两个数$N,L$. 接下来$N$行每行两个数$A_i,B_i$. 接下来$N$行每行一个整数$C_i$. 输出格式 一行一个整数表示答案,无解输出$-1$. ...

  3. python3笔记十四:python可变与不可变数据类型+深浅拷贝

    一:学习内容 python3中六种数据类型 python赋值 python浅拷贝 python深拷贝 二:python3六种数据类型 1.六种数据类型 Number(数字) string(字符串) L ...

  4. 【python】小型神经网络的搭建

    import numpy as np def sigmoid(x): # Sigmoid activation function: f(x) = 1 / (1 + e^(-x)) return 1 / ...

  5. git设置mergetool可视化工具

      可以设置BeyondCompare,DiffMerge等作为git的比较和合并的可视化工具,方便操作. 设置如下: 先下载并安装BeyondCompare,DiffMerge等. 设置git配置, ...

  6. leetcode 72. 编辑距离

    /***** 定义状态: DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符 DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数: 状态转移: for i: ...

  7. U盘无法拷贝大于4G的文件解决办法汇总

    U盘主要有三种格式: FAT32: 缺点:单个文件不能超过4GB,不支持512MB以下容量的U盘 备注:如果U盘容量达8GB以上,发现4GB文件拷不进去的话,可以考虑换用NTFS或ExFAT格式了 ● ...

  8. influxDB使用小结

    在集群中安装influxdb influxdb提供了官方镜像,因此在集群中安装influxdb十分方便,只需要指定镜像名为influxdb即可自动下载运行,只需要配置环境变量就可以进行初始化设置 以下 ...

  9. 三十六:数据库之SQLAlchemy外建之一对一关系

    relationship()的uselist参数默认为True,即一对多,如果要一对一,则需让uselist=False 准备工作 from sqlalchemy import create_engi ...

  10. loadrunner 场景设计-手工场景方案(Schedule)设计 Part 1

    参考:http://blog.sina.com.cn/s/articlelist_5314188213_1_1.html loadrunner 场景设计-手工场景方案(Schedule)设计 Part ...