题目链接:

Distance Queries

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 11531   Accepted: 4068
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 题意: 给一棵树;问两点之间的距离; 思路: lca的模板题,一开始莫名其妙的wa了,看了好久才发现是一个地方写反了;后来又tle,把cin改成scanf就好了;看来还是不能用cin; AC代码:
/*2014300227    1986    Accepted    13984K    485MS    G++    2077B*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=4e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,m,cnt,head[N],vis[N],a[*N],dep[N],first[N],dis[N],num,dp[*N][],p[N]; struct Edge
{
int to,next,val;
};
Edge edge[*N];
void add_edge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
int dfs(int x,int deep)
{
vis[x]=;
first[x]=num;
a[num++]=x;
dep[x]=deep;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
dis[y]=dis[x]+edge[i].val;
dfs(y,deep+);
a[num++]=x;
}
}
}
int RMQ()
{
for(int i=;i<num;i++)
{
dp[i][]=a[i];
}
for(int j=;(<<j)<=num;j++)
{
for(int i=;i+(<<j)-<num;i++)
{
if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
}
}
int query(int l,int r)
{
int temp=(int)(log((r-l+)*1.0)/log(2.0));
if(dep[dp[l][temp]]<dep[dp[r-(<<temp)+][temp]])return dp[l][temp];
else return dp[r-(<<temp)+][temp];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
num=;
for(int i=;i<=n;i++)
{
vis[i]=;
head[i]=-;
}
int u,v,w;
char s[];
for(int i=;i<m;i++)
{
scanf("%d%d%d%s",&u,&v,&w,s);
//cin>>u>>v>>w>>s;
add_edge(u,v,w);
add_edge(v,u,w);
}
dis[]=;
dfs(,);
RMQ();
int q,fx,fy;
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&fx,&fy);
int lca;
if(first[fx]<first[fy])
{
lca=query(first[fx],first[fy]);
}
else lca=query(first[fy],first[fx]);
printf("%d\n",dis[fx]+dis[fy]-*dis[lca]);
}
}
return ;
}
												

poj-1986 Distance Queries(lca+ST+dfs)的更多相关文章

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

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

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

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

  3. poj 1986 Distance Queries LCA

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

  4. 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 ...

  5. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

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

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

  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(Tarjan离线法求LCA)

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

  9. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

随机推荐

  1. java 接口回调

    学习自:http://blog.csdn.net/xiaanming/article/details/8703708/ http://hellosure.iteye.com/blog/1130176 ...

  2. centos-64整合nginx和tomcat

    centos-64整合nginx和tomcat 分类: Linux 2013-04-25 10:41 128人阅读 评论(0) 收藏 举报 1.安装wget和依赖包 yum install wget ...

  3. java 动态实现接口

    package com.yhouse.modules.daos; public interface IUserDao { public String getUserName(); public Str ...

  4. windows下如何快速优雅的使用python的科学计算库?

    Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...

  5. Centos 6.X noVNC+websockify 实现webvnc

    文章参考:https://github.com/kanaka/noVNC http://www.cnblogs.com/yanghuahui/p/3574388.html 工作原理: noVNC 可以 ...

  6. 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络

    content 概述 文字识别系统LeNet-5 简化的LeNet-5系统 卷积神经网络的实现问题 深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系 ...

  7. activemq 安装-单点

    一,准备工作:首先安装jdk1.7及其以上版本,此环境安装的是jdk-1.8   二.搭建activemq 环境:  192.168.9.25         centos6.5            ...

  8. mvc 各种返回值

    一个例子胜过千言万语,直接上代码 SpringMVC的Controller控制器返回值详解 SpringMVC Controller 返回值几种类型 Spring MVC 更灵活的控制 json 返回 ...

  9. 绿色版Tomcat的配置

    在环境变量中不配置JAVA_HOME或者JRE_HONE的情况下(正确配置java的路径)不影响java的使用 可以正常使用 java -version .... 但是这种情况下 无法在Tomcat的 ...

  10. SQL时间戳的使用(转)

    一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一个时间字段,每次增加数据时,填入当前的时间值.其实这误导了很多朋友. 1.基本概念 时间戳:数据库中自动生成的唯一二进制数字,与时间 ...