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. Supporting Connected Routes to Subnet Zero

    Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...

  2. java基本概念

    什么是环境变量? 环境变量通常是指在操作系统当中,用来指定操作系统运行时需要的一些参数.通常为一系列的键值对. path环境变量的作用 path环境变量是操作系统外部命令搜索路径 什么是外部命令搜索路 ...

  3. linux rm 命令

    1.命令格式: rm [选项] 文件… 2.命令功能: 删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录.如果使用 rm 来删除文件,通常仍可以将该文件恢复原状. 3. ...

  4. SystemServer相关

    SystemServer分析 由Zygote通过Zygote.forkSystemServer函数fork出来的.此函数是一个JNI函数,实现在dalvik_system_Zygote.c中. 1.S ...

  5. hdu 1434 幸福列车

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1434 幸福列车 Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长 ...

  6. Java使用JSP Tag Files & JSP EL Functions打造你自己的页面模板

    1. 简单说明:在JSP 2.0后, 你不再需要大刀阔斧地定义一堆TagSupport或BodyTagSupport, 使用JSP Tag Files技术可以实现功能强大的页面模板技术. 在这里抛砖引 ...

  7. [译]AMQP 0-9-1 Quick Reference : basic

    Basic basic.ack(delivery-tag delivery-tag, bit multiple)Support: fullAcknowledge one or more message ...

  8. How to write a windows service

    how to write a windows services susport microsoft This aritcle describe the detail step to setup a w ...

  9. 47.MIF和COE文件格式

    .mif和.coe这两个文件分别是Quartus和ISE的RAM和ROM的初始化文件,因此了解他们的格式,是很必要的   MIF文件的格式如下:   WIDTH=14; --数据宽度为14位 DEPT ...

  10. CString string char* char 之间的字符转换(多种方法)

    在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换.首先解释下三者的含义. CString 是一种很有用的数据类型.它们很大程度上简化了MF ...