Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 8638   Accepted: 3032
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. 

Source

 
lca
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector> using namespace std; const int MAX_N = ;
int N,M;
int first[MAX_N],Next[ * MAX_N],v[ * MAX_N];
int id[MAX_N],vs[ * MAX_N];
int dep[MAX_N * ],d[MAX_N * ][],qid[MAX_N * ][];
int Dis[MAX_N],w[MAX_N * ];
int n; void RMQ() {
for(int i = ; i <= n; ++i) {
d[i][] = dep[i];
qid[i][] = i;
} for(int j = ; ( << j) <= n; ++j) {
for(int i = ; i + ( << j) - <= n; ++i) {
if(d[i][j - ] > d[i + ( << (j - ))][j - ]) {
d[i][j] = d[i + ( << (j - ))][j - ];
qid[i][j] = qid[i + ( << (j - ))][j - ];
} else {
d[i][j] = d[i][j - ];
qid[i][j] = qid[i][j - ];
}
}
} } void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} int query(int L,int R) {
int k = ;
while(( << (k + )) < (R - L + )) ++k;
return d[L][k] < d[R - ( << k) + ][k] ?
qid[L][k] : qid[R - ( << k) + ][k];
} void dfs(int u,int fa,int d,int dis,int &k) {
id[u] = k;
vs[k] = u;
dep[k++] = d;
Dis[u] = dis;
for(int e = first[u]; e != -; e = Next[e]) {
if(v[e] != fa) {
dfs(v[e],u,d + ,dis + w[e],k);
vs[k] = u;
dep[k++] = d;
}
}
} int main()
{
// freopen("sw.in","r",stdin);
scanf("%d%d",&N,&M);
n = * N - ; for(int i = ; i <= N; ++i) first[i] = -;
for(int i = ; i <= * M; i += ) {
int u;
char ch;
scanf("%d%d%d %c",&u,&v[i],&w[i],&ch);
//printf("%d %d %d\n",u,v[i],w[i]);
w[i + ] = w[i];
v[i + ] = u;
add_edge(i,u);
add_edge(i + ,v[i]);
} int k = ;
dfs(,-,,,k);
RMQ(); int Q;
scanf("%d",&Q);
for(int i = ; i <= Q; ++i) {
int a,b;
scanf("%d%d",&a,&b);
int p = vs[ query(min(id[a],id[b]),max(id[a],id[b])) ];
printf("%d\n",Dis[a] + Dis[b] - * Dis[p]);
} return ;
}

poj 1986的更多相关文章

  1. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  2. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  3. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  4. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  5. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  6. POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]

    题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...

  7. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  8. POJ 1986:Distance Queries(倍增求LCA)

    http://poj.org/problem?id=1986 题意:给出一棵n个点m条边的树,还有q个询问,求树上两点的距离. 思路:这次学了一下倍增算法求LCA.模板. dp[i][j]代表第i个点 ...

  9. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

  10. POJ 1986:Distance Queries

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18139   Accepted: 6248 ...

随机推荐

  1. GNU make 规则

    clean : rm *.tmp 规则格式: targets : prerequisites recipe ... targets : prerequisites : recipe recipe .. ...

  2. 菜鸟学习Hibernate——简单的一个例子

    一.Hibernate开发. 上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类. 为了能够使用Hibernate快速上手,我们先讲解一个简单的Hibernate应用实例hibernate ...

  3. .NET开源工作流RoadFlow-流程设计-流程步骤设置-按钮设置

    按钮设置是配置当前步骤的处理者可以执行哪些操作,每个按钮都有对应的执行脚本(javascript脚本). 从左边的按钮列表中选择当前步骤需要的按钮. 注意:如果是流程最后一步则要配置完成按钮而不是发送 ...

  4. java 通过zxing生成二维码

    1.基本类提供二维码生成工具类 package com.green.util; import java.awt.image.BufferedImage; import java.io.ByteArra ...

  5. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  6. hdu 1212 Big Number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Big Number Description As we know, Big Number is ...

  7. jquery 简单弹出层

    预定义html代码:没有 所有代码通过js生成和移除. 预定义css .z-popup-overlay{ width:100%; min-height: 100%; height:800px; pos ...

  8. 在.net程序中使用System.Net.Mail来发送邮件

    System.Net.Mail是微软自家提供的工具,在.net程序中可以使用该空间中的SmtpClient实例来实现邮件的发送. 使用System.Net.Mail空间与Web.config配置相配合 ...

  9. 基于Elasticsearch开发时的注意事项备忘

    记录一些自己在Elasticsearch开发过程的琐碎知识点 1.使用ScriptFields时,需在yml配置文件中添加配置(script.disable_dynamic: false)开启动态脚本 ...

  10. bash: 避免命令重复执行的简单脚本

    1. 根据命令生成md5做为文件名保存当前进程的pid2. 使用exec执行命令3. 如果再次执行, 使用ps -p检测上次pid是否有效, 如果是则exit 200.否则重复1.hadoop@ubu ...