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:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e5+;
struct Edge{int v, w, nxt;}edge[MAXN<<];
struct Query
{
int v, id;
Query(){};
Query(int _v, int _id):v(_v),id(_id){};
};
vector<Query> q[MAXN]; int head[MAXN], cnt;
int dis[MAXN];
int fa[MAXN];
bool vis[MAXN];
int ans[MAXN];
int N, M, K; void init()
{
memset(vis, false, sizeof(vis));
memset(head, -, sizeof(head));
memset(dis, , sizeof(dis));
memset(ans, , sizeof(ans));
for(int i = ; i <= N; i++) q[i].clear();
cnt = ;
} int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);} void AddEdge(int from, int to, int weight)
{
edge[cnt].v = to;
edge[cnt].w = weight;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void dfs(int s, int f)
{
int root = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
if(edge[i].v == f) continue;
dis[edge[i].v] = dis[root] + edge[i].w;
dfs(edge[i].v, s);
}
} void Tarjan(int s, int f)
{
int root = s;
fa[s] = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
int Eiv = edge[i].v;
if(Eiv == f) continue;
Tarjan(Eiv, root);
fa[getfa(Eiv)] = root;
}
vis[s] = true;
for(int i = ; i < q[s].size(); i++){
if(vis[q[s][i].v] && ans[q[s][i].id] == ){
ans[q[s][i].id] = dis[q[s][i].v] + dis[s] - *dis[getfa(q[s][i].v)];
}
}
} int main()
{
scanf("%d%d", &N, &M);
init();
char s;
for(int i = , u, v, w; i <= M; i++){
scanf("%d%d%d %c", &u, &v, &w, &s);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
scanf("%d", &K);
for(int i = , u, v; i <= K; i++){
scanf("%d%d", &u, &v);
q[u].push_back(Query(v, i));
q[v].push_back(Query(u, i));
}
dfs(, -);
Tarjan(, -);
for(int i = ; i <= K; i++){
printf("%d\n", ans[i]);
}
return ;
}
上一篇:Android Studio 提高开发效率的插件


下一篇:POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)