poj 1986 Distance Queries(LCA求两点距离模版)

本文介绍了一种使用Tarjan算法求解无向图中两点间最短路径的方法,并提供了一个具体的AC代码实现。该算法适用于解决农民约翰的奶牛在不同农场间的距离计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:给出一个无向图,给出若干询问,每个询问求两点距离。
思路:tarjan连线算法求lca
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=40005;
struct node
{
    int v,w,next;
} edge[maxn*2];
struct node1
{
    int v,next,id;
} que[maxn];
int fa[maxn],G[maxn],Q[maxn],id[maxn],ans[maxn],dis[maxn];
bool vis[maxn];
int n,m,q,num,numq,cnt;
void init()
{
    memset(G,-1,sizeof(G));
    memset(Q,-1,sizeof(Q));
    memset(vis,false,sizeof(vis));
    num=numq=0;
}
void add(int u,int v,int w)
{
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=G[u];
    G[u]=num++;
}
void addq(int u,int v,int id)
{
    que[numq].v=v;
    que[numq].next=Q[u];
    que[numq].id=id;
    Q[u]=numq++;
}
int find_set(int x)
{
    return x==fa[x]?x:fa[x]=find_set(fa[x]);
}
void Union(int a,int b)
{
    int ra=find_set(a);
    int rb=find_set(b);
    if(ra!=rb) fa[rb]=ra;
}
void input()
{
    char s[3];
    int a,b,c;
    while(m--)
    {
        scanf("%d%d%d%s",&a,&b,&c,s);
        add(a,b,c);
        add(b,a,c);
    }
    scanf("%d",&q);
    for(int i=1; i<=q; i++)
    {
        scanf("%d%d",&a,&b);
        addq(a,b,i);
        addq(b,a,i);
    }
}
void dfs(int u)
{
    id[u]=cnt;
    fa[u]=u;
    vis[u]=true;
    for(int i=G[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
        dis[v]=dis[u]+edge[i].w;
        dfs(v);
        Union(u,v);
        }
    }
    for(int i=Q[u]; i!=-1; i=que[i].next)
    {
        int v=que[i].v;
        if(vis[v]&&id[u]==id[v]) ans[que[i].id]=dis[u]+dis[v]-2*dis[find_set(v)];
    }
}
void solve()
{
    memset(vis,false,sizeof(vis));
    memset(id,0,sizeof(id));
    for(int i=1;i<=n;i++)
    if(!vis[i])
    {
    dis[i]=0;
    dfs(i);
    cnt++;
    }
    for(int i=1;i<=q;i++)
    printf("%d\n",ans[i]);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        input();
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值