POJ1986 Distance Queries(最近公共祖先lca,离线Tarjan算法,最短路)
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
思路
首先说题意,给你n个点和m条边,组成一个无向连通图,然后接下来是m条边的信息,是起点终点和权值,后面的字幕忽略就行。然后给出q组询问,询问两点之间的最短距离。
很明显,这个不能用最短路的弗洛里德,因为有大量的查询,所以可以用最近公共祖先来做。
先处理一个每一个节点到根节点你的距离,对于每组询问,求出最近公共祖先lca
,很明显有这样的结论:
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000
#define mem(a,b) memset(a,b,sizeof(a))
const int N=400000+7;
int pre[N],first[N],first2[N],tot,tot2;
bool vis[N];//标记有没有询问
int n;
int dis[N],ans[N];
struct edge
{
int v,w,next;
} e[2*N];
struct Query
{
int v,next,id;
} query[2*N];
void add_edge(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
void add_query(int u,int v,int id)
{
query[tot2].id=id;
query[tot2].v=v;
query[tot2].next=first2[u];
first2[u]=tot2++;
}
int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}
void lca(int u,int fa)
{
for(int i=first[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(v==fa) continue;
lca(v,u);
pre[v]=u;
}
vis[u]=1;
for(int i=first2[u]; ~i; i=query[i].next)
{
int v=query[i].v;
if(vis[v])
{
int id=query[i].id;
ans[id]=dis[u]+dis[v]-2*dis[find(v)];
}
}
}
int vis2[N];
void dfs(int u,int len)
{
vis2[u]=1;
dis[u]=len;
for(int i=first[u]; ~i; i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(!vis2[v])
dfs(v,len+w);
}
}
void init()
{
mem(first,-1);
mem(first2,-1);
mem(vis,0);
mem(dis,0);
mem(vis2,0);
tot=0;
tot2=0;
for(int i=1; i<=n; i++)
pre[i]=i;
}
int main()
{
int m,q;
int u,v,w;
char c[5];
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1; i<=m; i++)
{
scanf("%d%d%d%s",&u,&v,&w,c);
add_edge(u,v,w);
add_edge(v,u,w);
}
scanf("%d",&q);
for(int i=1; i<=q; i++)
{
scanf("%d%d",&u,&v);
add_query(u,v,i);
add_query(v,u,i);
}
dfs(1,0);
lca(1,1);
for(int i=1; i<=q; i++)
printf("%d\n",ans[i]);
}
return 0;
}
上一篇: HBase之rowkey设计原则和方法