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. EmguCV学习——简单使用

    关于EmguCV我就不多说了,是对应于OpenCV的一套net库. 公司是视觉方面的业务,我又不会c++(好想会啊,正在学习中).由于各种需求,自己觉得对c++不是特别感冒,所以选用了net下的ope ...

  2. GestureDetector.OnGestureListener

    为了加强鼠标响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Ta ...

  3. linux清除swap

    执行top会显示Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%stMem:   2044500 ...

  4. iOS中MVC设计模式

    在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...

  5. H5 input type="search" 不显示搜索 解决方法

    在IOS(ipad iPhone等)系统的浏览器里打开H5页面.如下写法: <input type="search" name="search” id=" ...

  6. IIS8 web.config 重定向之后 报错 500.19

    原因是没有安装 URL Rewrite 官方下载地址:http://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads

  7. As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource

    默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...

  8. HTML5做的浏览器欢迎界面自动跳转

    HTML5做的浏览器欢迎界面自动跳转 思路很简单,随手装逼呗.根据时间来控制背景图和文字,背景图加了毛玻璃效果,效果直接看图,用javascript来实现. 完整代码 <!DOCTYPE htm ...

  9. 丢掉 WinPE,使用 DISKPART 来分区吧

    自 Windows Vista 之后的操作系统,如果在安装系统的时候使用 Windows 自带的分区功能,则会多出一个 100M 的系统保留分区.这会让一个物理硬盘,原先最多可以分 4 个主分区的,现 ...

  10. 20145129 《Java程序设计》第6周学习总结

    20145129 <Java程序设计>第6周学习总结 教材学习内容总结 InputStream与OutStream 串流设计的概念 输入串流代表对象为java.io.InputStream ...