例题: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 ;
}

最新文章

  1. javaScript笔记
  2. 深入理解Objective-C Runtime
  3. 在SecureCRT中使用rz和sz传输文件
  4. KMP算法模板
  5. seleniu IDE 点点滴滴
  6. TSharding:用于蘑菇街交易平台的分库分表组件
  7. iOS: 学习笔记, Swift名字空间
  8. Django模板-分离的模板
  9. dd的用法
  10. [模拟赛] T2 混合图
  11. Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher) from AndroidManifest
  12. java----OO的概念和设计原则(转)
  13. 2017-04-26 ios ipv6那些事(已完美解决)
  14. 集合之fail-fast机制
  15. why pure virtual function has definition 为什么可以在基类中实现纯虚函数
  16. Android SDK代理server解决国内不能更新下载问题
  17. linux系统mysql连接检查脚本
  18. 【bzoj4753】[Jsoi2016]最佳团体 分数规划+树形背包dp
  19. Neural Networks and Deep Learning学习笔记ch1 - 神经网络
  20. LCA UESTC 92 Journey

热门文章

  1. CERN Root与CLING
  2. 力软敏捷框架7.0.6 葡萄城报表升级到ar14版本
  3. Spring Framework之IoC容器
  4. C++ STL模板和标准模板库
  5. TCP漫谈之keepalive和time_wait
  6. 【WPF学习】第六十二章 构建更复杂的模板
  7. HBase 监控 | HBase Metrics 初探(一)
  8. B 基因改造
  9. background-clip 和 background-origin 有什么区别? -[CSS] - [属性]
  10. Light of future-冲刺Day 7