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. oracle11g rman验证备份有效性

    RMAN> restore validate controlfile; Starting restore at 21-NOV-13using target database control fi ...

  2. Windows Phone动画

    从事Windows Phone开发已经有一段时间了,但是一直没有好好的静下心来梳理一下自己这段时间的知识,一是怕自己学问不到家,写不出那些大牛一般的高屋建瓴:二是以 前一直没有写博客的习惯:好了废话不 ...

  3. 在linux上使用yum安装JDK

    1.查找java相关得列表 [qyf@localhost ~]$ yum -y list java* 执行结果 [qyf@localhost ~]$ yum -y list java* Loaded ...

  4. 获得N位数字字母随机组合

    import string import random def get_rand(n): allw = string.letters+string.digits r = [] for i in ran ...

  5. 企业该如何进行高效IT运维管理

    企业该如何进行高效IT运维管理 在企业内部也是一样,当大量的生产和经营数据集中在数据中心,一旦人们与数据中心因为IT故障而失去联系,停滞的也许不是个人应用受阻这样简单的后果.我们谁也不想看到自己企业的 ...

  6. JavaScript AJAX stream 流式显示

      当使用AJAX进行信息交互的时候,如果服务器返回的信息比较大,那么相对于传送完成之后的统一显示,流式显示就比较友好了. 流式实现 原理就是设置定时器,定时的查看AJAX对象的状态并更新内容,如果传 ...

  7. ios8中的UIScreen

    let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...

  8. php xml转为xml或者json

    <?php class XmlToArray { private $xml; private $contentAsName="content" ; private $attr ...

  9. 56.ISE综合,在chipscope信号列表看不到

    代码写好后,进行逻辑综合,在chipscope上添加被触发的信号时,发现有些在信号列表里看不到,这是因为这些信号没有参与到逻辑电路设计中,产生不想关的电路,综合器会默认优化资源. 还有一种情况是,对于 ...

  10. NSNumber、NSValue、NSDate、NSObject

    注:OC中数组和字典只能存储OC对象不能存放基本数据类型. NSNumber NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象. NSNumber *number = [ ...