POJ 1986 Distance Queries【LCA】

Distance Queries

Time Limit: 2000MS

 

Memory Limit: 30000K

Total Submissions: 11677

 

Accepted: 4124

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. 

Source

USACO 2004 February

 

题目大意:John是一个农场主,他的牛懒散惯了,不想根据他规定的线路走,所以他需要知道每两个节点之间的最短距离,有那么些个询问,求这些个询问中两个节点之间距离。


思路:


1、因为无论哪个节点在哪个节点的那个方向,最终能够形成一棵树。所以我们不需要考虑方向的问题。


2、因为节点比较多,询问比较多,使用单源最短路之类的算法比较鸡肋。我们选择离线的LCA-tarjan算法。时间复杂度为(n+q);


3、因为是无向图,所以根节点的确定变得很蛋疼,不过我们可以建立双向图,这个时候我们就可以定任何一个点为根节点了,这里我们不妨方便设定为1。


4、因为解决LCA问题的时候我们采用递归+并查集的方法来解决,这个时候我们有两种元素,一种是当前节点u,一种是当前直接与当前节点相连的子节点v。因为是递归过程求解,所以我们不妨根据这个特性来统计dir【i】(表示i到根节点的距离)。在递归过程中不难理解dir是可以这样求的:dir【v】=dir【u】+w(u,v);那么我们有了这个元素,我们也不难理解有这样的一个递推式:dis(x,y)=dis(x,root)+dis(y,root)-2*(LCA(x,y),root);这样,我们就可以轻松解决整个题了。


因为节点数比较多,所以用邻接矩阵是行不通的,这里采用链式前向星来实现静态邻接表解决。

AC代码:


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int head[100000];
int f[100000];
int qhead[100000];
int vis[100000];
int dir[100000];
int problem[100000][2];
struct EdgeNode
{
    int from;
    int to;
    int w;
    int lca;
    int next;
}e[100000],q[100000];
int n,m,ask,cont;
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont;
    cont++;
    e[cont].from=to;
    e[cont].to=from;
    e[cont].w=w;
    e[cont].next=head[to];
    head[to]=cont;
    cont++;
}
void addq(int from,int to)
{
    q[cont].from=from;
    q[cont].to=to;
    q[cont].lca=-1;
    q[cont].next=qhead[from];
    qhead[from]=cont;
    cont++;
    q[cont].from=to;
    q[cont].to=from;
    q[cont].lca=-1;
    q[cont].next=qhead[to];
    qhead[to]=cont;
    cont++;
}
void LCA(int u)
{
    f[u]=u;
    vis[u]=1;
    for(int k=head[u];k!=-1;k=e[k].next)
    {
        int to=e[k].to;
        if(vis[to]==0)
        {
            int w=e[k].w;
            dir[to]=dir[u]+w;
            LCA(to);
            merge(u,to);
        }
    }
    for(int k=qhead[u];k!=-1;k=q[k].next)
    {
        int to=q[k].to;
        if(vis[to]==1)
        {
            q[k].lca=find(to);
            q[k^1].lca=q[k].lca;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    cont=0;
    for(int i=1;i<=n;i++)f[i]=i;
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    memset(qhead,-1,sizeof(qhead));
    memset(dir,0,sizeof(dir));
    for(int i=0;i<m;i++)
    {
        int x,y,w;char s[50];
        scanf("%d%d%d%s",&x,&y,&w,s);
        add(x,y,w);
    }
    scanf("%d",&ask);
    cont=0;
    int tot=0;
    for(int i=0;i<ask;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        addq(x,y);
    }
    LCA(1);
    for(int i=0;i<ask;i++)
    {
        int tmp=i*2,u=q[tmp].from,v=q[tmp].to,lca=q[tmp].lca;
        printf("%d\n",dir[u]+dir[v]-2*dir[lca]);
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值