poj1986Distance Queries

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

思路:LCA离线模板

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
const int N=80010;
const int M=20010;
struct node
{
    int u,v,w,next;
}e[N],que[N];
int head[N],a[N],dis[N],f[N],h[N],vis[N];
int num,cnt;
int n,m,q;
void init()
{
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
    memset(h,-1,sizeof(h));
    memset(a,0,sizeof(a));
    memset(f,0,sizeof(f));
    memset(vis,-1,sizeof(vis));
    num=cnt=0;
}
void addedge(int u,int v,int w)
{
    e[num].v=v;
    e[num].w=w;
    e[num].next=head[u];
    head[u]=num++;
}
void addque(int u,int v,int w)
{
    que[cnt].v=v;
    que[cnt].w=w;
    que[cnt].next=h[u];
    h[u]=cnt++;
}
int findx(int x)
{
    if(x==f[x])return x;
    return f[x]=findx(f[x]);
}
void dfs(int u,int w,int r)
{
    dis[u]=w;
    vis[u]=r;
    f[u]=u;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].v;
        if(vis[v]==-1)
        {
            dfs(v,w+e[i].w,r);
            f[v]=u;
        }
    }
    for(int i=h[u];i!=-1;i=que[i].next)
    {
        int v=que[i].v;
        if(vis[v]!=-1)
        {
            a[que[i].w]=dis[u]+dis[v]-2*dis[findx(v)];
        }
    }
}
int main()
{
    char s[3];
    while(~scanf("%d%d",&m,&n))
    {
        int i;
        int u,v,w;
        init();
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%s",&u,&v,&w,s);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            scanf("%d%d",&u,&v);
            addque(u,v,i);
            addque(v,u,i);
        }
        for(i=1;i<=m;i++)
        {
            if(vis[i]==-1)
            {
                dfs(i,0,i);
            }
        }
        for(i=0;i<q;i++)
        {
            printf("%d\n",a[i]);
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值