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. BFS(最短路) HDOJ 4308 Saving Princess claire_

    题目传送门 题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路 分析:一道最短路问题,加了传送门的功能, ...

  2. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  3. java问题收集

    2014-10-27 构造器最好保留一个无参的,否则一些框架调用初始化时,会报错     星期三,2013年11月6日 volatile关键字 : 1. 与synchronized几乎相同,但是vol ...

  4. 204 Count Primes 计数质数

    计算所有小于非负整数 n 的质数数量. 详见:https://leetcode.com/problems/count-primes/description/ Java实现: 埃拉托斯特尼筛法:从2开始 ...

  5. 帮助新手理解equals和hashCode

    入行快要两年,偶尔想起来equals和hash还是会有些晕,索性今天就更深入的弄明白一些,不足之处也请各位大神指出批评,共同进步. 刚开始学java的时候只是记忆性的来背,如果一个类在程序中可能进行比 ...

  6. AlertDialog的实现

    课程Demo 重点解析自定义对话框 public class MainActivity extends AppCompatActivity { private Button bt1; private ...

  7. Scala基础篇-04 try表达式

    1.try表达式 定义 try{} catch{} finally{} //例子 try{ Integer.parseInt("dog") }catch { }finally { ...

  8. 把Scheme翻译成Java和C++的工具

    一.为什么要写这个工具? 公司内容有多个项目需要同一个功能,而这些项目中,有的是用Java的,有的是用C++的,同时由于某些现实条件限制,无法所有项目都调用统一的服务接口(如:可能运行在无网络的情况下 ...

  9. iOS---UICollectionView详解和常用API翻译

    UICollectionView 1.必须要设置布局参数 2.注册cell 用法类似于UITableView 类.自动实现重用,必须注册初始化. 使用UICollectionView必须实现UICol ...

  10. Ghost Win10系统X64位和32位10041装机版下载

    更多系统下载尽在系统妈:http://www.xitongma.com 特别说明: 1.C:盘分区须至少15GB(安装过程有大量的解压临时文件),安装完成后C:盘占用10GB左右! 2.安装之后如有硬 ...