例题:http://poj.org/problem?id=1986

 POJ1986 Distance Queries

Language:
Default
Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 18225   Accepted: 6265
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 
 

带权值的LCA处理方法和原处理方法唯一一点区别就是加了一个数组step[x]用来记录到根节点的距离,最后step[x]+step[y]-2*step[point]其中point为x和y的最近公共祖先点

AC代码:

#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+;
ll bits[];
ll depth[N];
int fa[N][];
struct stu {
int a,b;
};
vector<stu>ve[N];
int n,m;
int mark[N];
ll step[N];
void inint(){
bits[]=;
for(int i=;i<=;i++)
bits[i]=bits[i-]<<;
} void dfs(int x,int y){
depth[x]=depth[y]+;
fa[x][]=y;
for(int i=;i<ve[x].size();i++){
if(ve[x][i].a==y){
step[x]=step[y]+ve[x][i].b;
}
}
for(int i=;i<;i++) fa[x][i]=fa[fa[x][i-]][i-];
for(int i=;i<ve[x].size();i++){
int x1=ve[x][i].a;
if(x1!=y){
dfs(x1,x);
}
}
} int lca(int x,int y){ if(depth[x]<depth[y]) swap(x,y);
int dif=depth[x]-depth[y];
for(int i=;i>=;i--){
if(dif>=bits[i]){
x=fa[x][i];
dif-=bits[i];
}
}
if(x==y) return x;
for(int i=;i>=;i--){
if(depth[x]>=bits[i]&&fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
int main(){
inint();
scanf("%d%d",&n,&m);
int x,y,z;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
getchar();getchar();
ve[x].push_back({y,z});
ve[y].push_back({x,z});
}
dfs(,);
int d;
scanf("%d",&d);
while(d--){
int x1,y1;
scanf("%d%d",&x1,&y1);
int point=lca(x1,y1);
printf("%d\n",step[x1]+step[y1]-*step[point]);
}
return ;
}

带权值的LCA的更多相关文章

  1. 51nod1459(带权值的dijkstra)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...

  2. HDU 1863:畅通project(带权值的并查集)

    畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 洛谷 P2024 [NOI2001]食物链——带权值的并查集维护

    先上一波题目 https://www.luogu.org/problem/P2024 通过这道题复习了一波并查集,学习了一波带权值操作 首先我们观察到 所有的环都是以A->B->C-> ...

  4. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  5. nyoj-----284坦克大战(带权值的图搜索)

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 Many of us had played the game "Battle city" ...

  6. 带权值的图 BFS

    用bfs遍历最图求最短路径时通常借用优先队列即优先考虑最大的或者最小的权值 方法1 优先队列:(内置函数,优先考虑较小的权值) #include<iostream> #include< ...

  7. P - 奔小康赚大钱 - hdu 2255(带权值的匹配)

    分析:这是一个KM的模板题,也就不多说了,KM最复杂的情况都能过,下面是没有优化过的代码: ****************************************************** ...

  8. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  9. poj1986带权lca

    lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...

随机推荐

  1. Servlet(三)----Servlet体系与HTTP

    ## Servlet的体系结构 Servlet  ---  接口 | | GenericServlet  ---  抽象类 | | HttpServlet  --  抽象类 GenericServle ...

  2. Linux系统c语言开发环境

    项目 内容 这个作业属于哪个课程 班级地址 这个作业要求在哪里 作业要求地址 学号-姓名 17041506-张政 学习目标 Linux系统下C语言开发环境搭建,学习Linux系统环境C语言开发过程 L ...

  3. java,jq,ajax写分页

    1.先写好html基础样式 我懒得去写css样式233,能看就行 <style> #page { width: 20px; } </style> <table> & ...

  4. Springcloud 整合Hystrix 断路器,支持Feign客户端调用

    1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...

  5. [leetcode] 树 -Ⅰ

    均为 Simple 难度的水题. 二叉树的中序遍历 题目[94]:给定一个二叉树,返回它的中序 遍历. 解题思路:Too simple. class Solution { public: vector ...

  6. [vijos]1083小白逛公园<线段树>

    描述 小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了. 一开始,小白就根据公园的风景给每个公 ...

  7. A. Array with Odd Sum Round #617(水题)

    A. Array with Odd Sum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. 1040 Longest Symmetric String (25分)(dp)

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  9. PTA数据结构与算法题目集(中文) 7-10

    PTA数据结构与算法题目集(中文)  7-10 7-10 公路村村通 (30 分)   现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低 ...

  10. composer换源,composer切换国内源

    首先要分清楚是局部换源还是全局换源 1.局部换源(仅对当前项目有效) 在当前项目下的composer.json中添加 { "repositories": [ { "typ ...