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. 在WPF中调用Winform控件

    最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...

  2. jquery的异步获取返回值为中文时乱码解决方法

    用jqgrid异步获取列表值,遇到个问题,服务器端从数据库取到的数据没有出现中文乱码问题(日志打出来是没有乱码的),但是异步传到客户的时候却出现了乱码. 服务器端已经编码过了(UTF-8编码).开始一 ...

  3. System V共享内存区

    要点 shell查看命令:ipcs -m 主要函数 #include <sys/shm.h> //oflag=IPC_CREAT|IPC_EXCL|0644组合 //创建一个内存共享区 i ...

  4. [原创]PostgreSQL Plus Advanced Server监控工具PEM(四)

    四.PEM管理工具 1.编辑配置 选择进行管理的目标服务器,点击菜单Tools->Server Configuration->postgresql.conf管理服务器配置,可以看到我们经常 ...

  5. golang的序列与反序列化

    golang写backend之类的应用,还是挺方便的...使用encoding/json包时, 必须注意, 在struct定义的属性必须是exported, 否则不会设置值. 例如:type DRol ...

  6. andirod

    于adnroid老手来说,SDK环境搭建是很简单的,但是对于我这样的小白来说,,,,,走啦很多弯路..特记下.希望对兄弟们有所帮助 因为我也是参考网上的高手知道,所以有的就直接复制啦,,^_^ 想要开 ...

  7. [转]理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  8. Eclipse Indigo 3.7.0 安装GIT插件提示 requires 'bundle org.eclipse.team.core(转)

    文章参考来源:http://showlike.iteye.com/blog/1958538 错误提示: Cannot complete the install because one or more ...

  9. 在51系列中data,idata,xdata,pdata的区别

    在51系列中data,idata,xdata,pdata的区别: data:固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码 也最小. idata:固定指前面 ...

  10. PHP URL 重定向 的三种方法(转载)

    为了方便查询,转载一篇. 1.使用header()函数    PHP的HTTP相关函数种提供了一个 header()函数,首先要清楚,header()函数必须放在php程序的开头部分,而且之前不能有另 ...