POJ 1986 Distance Queries 输入YY && LCA(Tarjan离线)

Posted ymzjj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1986 Distance Queries 输入YY && LCA(Tarjan离线)相关的知识,希望对你有一定的参考价值。

任意门:http://poj.org/problem?id=1986

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 16648   Accepted: 5817
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. 

 

题意概括:

输入:

第一行输入结点数 N  和 边数 M。

接下来 M 行输入边的信息:起点 u 终点 v 距离 w 方向 s

输入查询数 K

接下来 K 行 输入查询值: 起点 u,终点 v;

 

解题思路:

一个有向无环图,当作一棵树来处理,根结点随意,假设为 1;

LCA 两点最短距离 老套路。

 

AC code:

技术分享图片
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <cstring>
 6 #define INF 0x3f3f3f3f
 7 #define LL long long
 8 using namespace std;
 9 const int MAXN = 5e5+5;
10 struct Edge{int v, w, nxt;}edge[MAXN<<1];
11 struct Query
12 {
13     int v, id;
14     Query(){};
15     Query(int _v, int _id):v(_v),id(_id){};
16 };
17 vector<Query> q[MAXN];
18 
19 int head[MAXN], cnt;
20 int dis[MAXN];
21 int fa[MAXN];
22 bool vis[MAXN];
23 int ans[MAXN];
24 int N, M, K;
25 
26 void init()
27 {
28     memset(vis, false, sizeof(vis));
29     memset(head, -1, sizeof(head));
30     memset(dis, 0, sizeof(dis));
31     memset(ans, 0, sizeof(ans));
32     for(int i = 0; i <= N; i++) q[i].clear();
33     cnt = 0;
34 }
35 
36 int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
37 
38 void AddEdge(int from, int to, int weight)
39 {
40     edge[cnt].v = to;
41     edge[cnt].w = weight;
42     edge[cnt].nxt = head[from];
43     head[from] = cnt++;
44 }
45 
46 void dfs(int s, int f)
47 {
48     int root = s;
49     for(int i = head[s]; i != -1; i = edge[i].nxt){
50         if(edge[i].v == f) continue;
51         dis[edge[i].v] = dis[root] + edge[i].w;
52         dfs(edge[i].v, s);
53     }
54 }
55 
56 void Tarjan(int s, int f)
57 {
58     int root = s;
59     fa[s] = s;
60     for(int i = head[s]; i != -1; i = edge[i].nxt){
61         int Eiv = edge[i].v;
62         if(Eiv == f) continue;
63         Tarjan(Eiv, root);
64         fa[getfa(Eiv)] = root;
65     }
66     vis[s] = true;
67     for(int i = 0; i < q[s].size(); i++){
68         if(vis[q[s][i].v] && ans[q[s][i].id] == 0){
69             ans[q[s][i].id] = dis[q[s][i].v] + dis[s] - 2*dis[getfa(q[s][i].v)];
70         }
71     }
72 }
73 
74 int main()
75 {
76     scanf("%d%d", &N, &M);
77     init();
78     char s;
79     for(int i = 1, u, v, w; i <= M; i++){
80         scanf("%d%d%d %c", &u, &v, &w, &s);
81         AddEdge(u, v, w);
82         AddEdge(v, u, w);
83     }
84     scanf("%d", &K);
85     for(int i = 1, u, v; i <= K; i++){
86         scanf("%d%d", &u, &v);
87         q[u].push_back(Query(v, i));
88         q[v].push_back(Query(u, i));
89     }
90     dfs(1, -1);
91     Tarjan(1, -1);
92     for(int i = 1; i <= K; i++){
93         printf("%d
", ans[i]);
94     }
95     return 0;
96 }
View Code

 





以上是关于POJ 1986 Distance Queries 输入YY && LCA(Tarjan离线)的主要内容,如果未能解决你的问题,请参考以下文章

poj1986 Distance Queries

POJ——T 1986 Distance Queries

POJ1986 Distance Queries (LCA)(倍增)

POJ-1986 Distance Queries(LCA离线)

POJ 1986 Distance Queries(LCA)

poj1986Distance Queries(RMQ+LCA)