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.
InputFirst 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.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 - 求任意两点间的距离的更多相关文章

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

  2. HDU2586(LCA应用:在带权树中求任意两点之间的距离)

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

  3. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  4. Floyed-Warshall算法(求任意两点间最短距离)

    思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...

  5. 计算GPS两点间的距离[单位为:米]

    /**     * 计算GPS两点间的距离[单位为:米]     * @param center GPS当前数据(LonLat对象表示,LonLat.lon表示经度,LonLat.lat表示纬度)   ...

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

  7. 图算法之Floyd-Warshall 算法-- 任意两点间最小距离

    1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...

  8. HDOJ2001计算两点间的距离

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. HDOJ/HDU 2547 无剑无我(两点间的距离)

    Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...

随机推荐

  1. Django入门9--Django shell

  2. springboot配置大全

    此配置大全是在官方开发者文档中看到的,地址:https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/common-ap ...

  3. substring和substr的区别和使用

    第一反应是都是截取字符串的方法,好像平常使用的时候也没太注意区分这俩,今天看到正好来区分一下 substring(start,[end]) 如果省略end,那么截取的是从指定位置到末尾 var str ...

  4. jdk+tomcat+mysql一键安装脚本

    最近在搞一个web项目部署,每次都要安装jdk.配置环境变量.安装tomcat和mysql.对于非开发人员,还是有点难度的,经常出错,然后就整理了一个自动化的脚本. JDKinstall.bat @e ...

  5. 【转载】VS Code 中的代码自动补全和自动导入包

    原文连接:https://maiyang.me/post/2018-09-14-tips-vscode/ VSCode 必须安装以下插件: 首先你必须安装 Golang 插件,然后再给 Go 安装工具 ...

  6. Linux网络IO模型

    同步和异步,阻塞和非阻塞 同步和异步 关注的是结果消息的通信机制 同步:同步的意思就是调用方需要主动等待结果的返回 异步:异步的意思就是不需要主动等待结果的返回,而是通过其他手段比如,状态通知,回调函 ...

  7. 使用BFD检测EBGP邻居

    在广域网BGP环境中,通常使能BFD以快速实现链路故障后的路由的主动收敛. BFD使用UDP在链路上进行双向检测,BFD有Echo mode和asynchronous mode两种模式,默认为Echo ...

  8. 学习Java第八周

    1.流的分类 1.字节流:Stream 2.字符流: Writer,Reader 输入流 :InputStream ,Reader 输出流 :OuputStream,Writer 字节流重要还是字符流 ...

  9. Django 中配置MySQL数据库

    在Django的项目中会默认使用sqlite的数据库 配置MySQL需要在setting.py 里加入以下设置: 配置数据库 DATABASES = { 'default': { 'ENGINE': ...

  10. Java_地铁购票系统

    定义了两个类,在Subway类中定义三个私有数据变量,线路号,经过站点,换乘站.以及4个方法分别实现从txt文件中导入线路信息:输出线路信息:查询两个站点经过站点数,并输出经过站点以及在某站换乘几号线 ...