Distance Queries

Time Limit: 2000ms
Memory Limit: 30000KB

This problem will be judged on PKU. Original ID: 1986
64-bit integer IO format: %lld      Java class name: Main

 
 
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

Source

 
解题:LCA求树上任意两点间的距离。任意两点间只有一条路啊!不然那还是树么?^_^。。。。
 
LCA(a,b) = c所以d(a,b) = d(a,root)+d(b,root)-2*d(c,root);人字形?嘻嘻!傻逼。。。。。当时我居然不能明白这个
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,w;
};
struct query{
int to,id;
};
int n,m,k,ans[maxn],d[maxn],uf[maxn];
bool vis[maxn];
vector<arc>g[maxn];
vector<query>q[maxn];
int Find(int x){
if(x != uf[x])
uf[x] = Find(uf[x]);
return uf[x];
}
void tarjan(int u,int ds){
vis[u] = true;
d[u] = ds;
uf[u] = u;
int i;
for(i = ; i < g[u].size(); i++){
if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;}
}
for(i = ; i < q[u].size(); i++)
if(vis[q[u][i].to]){
ans[q[u][i].id] = d[u]+d[q[u][i].to]-*d[Find(q[u][i].to)];
}
}
int main(){
int i,j,u,v,w;
char ch;
while(~scanf("%d %d",&n,&m)){
for(i = ; i <= n; i++){
g[i].clear();
q[i].clear();
d[i] = ;
vis[i] = false;
}
for(i = ; i < m; i++){
scanf("%d %d %d %c",&u,&v,&w,&ch);
g[u].push_back((arc){v,w});
g[v].push_back((arc){u,w});
}
scanf("%d",&k);
for(i = ; i < k; i++){
scanf("%d %d",&u,&v);
q[u].push_back((query){v,i});
q[v].push_back((query){u,i});
}
tarjan(,);
for(i = ; i < k; i++)
printf("%d\n",ans[i]);
}
return ;
}
 
 

BNUOJ 2105 Distance Queries的更多相关文章

  1. POJ1986 Distance Queries (LCA)(倍增)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12950   Accepted: 4577 ...

  2. POJ 1986 Distance Queries(Tarjan离线法求LCA)

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

  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)

    Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...

  5. 【LCA求最近公共祖先+vector构图】Distance Queries

    Distance Queries 时间限制: 1 Sec  内存限制: 128 MB 题目描述 约翰的奶牛们拒绝跑他的马拉松,因为她们悠闲的生活不能承受他选择的长长的赛道.因此他决心找一条更合理的赛道 ...

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

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

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

  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. 配置Ubuntu16.04第02步:更改镜像源,更新系统

    Ubuntu 16.04下载软件速度有点慢,因为默认的是从国外下载软件,那就更换到国内比较好的快速更新源(就是这些软件所在的服务器),一般直接百度Ubuntu更新源就能出来一大堆,这时候最好是找和自己 ...

  2. 使用Navicat迁移MySQL数据至Oracle时大小写原因报“表或视图不存在”问题处理

    使用Navicat提供的数据传输工具将JEECMSv9的MySQL的数据迁移至Oracle数据库,数据迁移成功表都存在,但是在程序启动时提示表或视图不存在. Caused by: java.sql.S ...

  3. Styles and Themens(1)详述

    Styles and Themes IN THIS DOCUMENT Defining Styles Inheritance Style Properties Applying Styles and ...

  4. linux安装glassfish并布署

    1 https://glassfish.java.net/download.html 2 准备工作:需要jdk7以上版本 Java EE 7 requires JDK 7 (or above) 下载g ...

  5. Mac OS X:在标题栏上显示目录完整路径

    众所周知mac的finder是不带路径显示的,你进入某个文件夹只会显示当前文件夹的名字而已.虽然你可以在finder的菜单栏中点“显示”-“显示路径栏”把路径栏调出来,但是这样只会不必要的增加find ...

  6. CoreText的绘制流程-转

    来自:http://blog.sina.com.cn/s/blog_7c8dc2d50101lbb1.html 使用coreText进行文本绘制,需要在工程中添加CoreText.framework, ...

  7. MySQL-时间(time、date、datetime、timestamp和year)

    情景进入 情境进入: 今天调试某查询页面,偶尔发现一个问题,刚刚插入的数据,没有正常排序显示,经过后台调试sql,发现一个问题??? 经过上面红色对比,不知道你发现问题没,Order by 只是多一个 ...

  8. 轻松搞定Spring+quartz的定时任务

    1.spring 的定时任务写法有两种:一种是继承工作类,一种是普通的Bean,定时写法有两种写法:一种是以时间间隔启动任务SimpleTriggerBean,一种是以时刻启动任务CronTrigge ...

  9. greenplum4.3.8.2安装

    GREENPLUM总体结构:   数据库由Master Severs和Segment Severs通过Interconnect互联组成. Master主机负责:建立与客户端的连接和管理:SQL的解析并 ...

  10. Sass的的使用三

    [Sass]普通变量与默认变量普通变量定义之后可以在全局范围内使用. 默认变量sass 的默认变量仅需要在值后面加上 !default 即可.sass 的默认变量一般是用来设置默认值,然后根据需求来覆 ...