POJ1986--Distance Queries

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
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn 200080
int first[maxn],nxt[maxn],vv[maxn],ww[maxn];
int qfirst[maxn],qnxt[maxn],quu[maxn],qvv[maxn],qans[maxn];
int f[100010],dis[100010];
int n,m,cnt,question,e;
bool vis[100100];

void addEdge(int u,int v,int d)///边他是存双向边的
{
        vv[e] = v;	ww[e] = d;		nxt[e] = first[u];	first[u] = e++;
		vv[e] = u;	ww[e] = d;		nxt[e] = first[v];		first[v] = e++;
}

void addQue(int u,int v)这个就是询问嘛,这个还是等下再改吧
{
        quu[cnt] = u;		qvv[cnt] = v;		qnxt[cnt] = qfirst[u];	qfirst[u] = cnt++;
		quu[cnt] = v;		qvv[cnt] = u;		qnxt[cnt] = qfirst[v];	qfirst[v] = cnt++;
}	

void Init()
{
        scanf("%d%d",&n,&m);
        memset(first,-1,sizeof(first));
		memset(qfirst,-1,sizeof(qfirst));
		memset(vis,0,sizeof(vis));
        e = cnt = 0;
        int x,y,d;
        char temp;
        for (int i=1;i<=m;i++)//树一个边表
        {
                scanf("%d %d %d %c\n",&x,&y,&d,&temp);
                addEdge(x,y,d);
        }这里存双向边。
        scanf("%d",&question);
        for (int i=1;i<=question;i++)//询问一个边表
        {
                scanf("%d%d",&x,&y);
				addQue(x,y);
        }
}

int find(int x)//就是find函数嘛
{
        if (f[x]==x) return x;
        f[x]=find(f[x]);
        return f[x];
}

void Union(int x,int y)//合并点
{
        int fx=find(x);
        f[y]=fx;father[y] = find(x);也就是说x和y的最近公共祖先
}

void Dfs(int u,int fa)//Dfs找最近公共祖先
{
        f[u]=u;
        for (int i=first[u];i != -1;i = nxt[i])
        {
                if (vv[i] == fa) continue;map就是边
                dis[vv[i]]=dis[u]+ww[i];深搜求出每个点到根的距离,这个我也知道的。
                Dfs(vv[i],u);
                Union(u,vv[i]);father[v] = find(u)....这样干的目的是什么                
        }
        vis[u]=1;b[x] 又是用来标记什么用的
        for (int i = qfirst[u];i  != -1;i = qnxt[i])//记录每组询问的最近公共祖先
                if (vis[qvv[i]]) qans[i] =find(qvv[i]);
}

void Main()
{
        for (int i=1;i<=n;i++)
        if (vis[i]==false) Dfs(i,-1);
        int x,y,ans;
        for (int i=0;i<question*2;i+=2)
        {
                x=quu[i];
                y=qvv[i];
                ans=qans[i];
                if (qans[i+1]>qans[i])
                {
                        ans=qans[i+1];
                }
                printf("%d\n",dis[x]+dis[y]-2*dis[ans]);//输出距离
        }
}

int main()
{
		//freopen("in.txt","r",stdin);
        Init();
        Main();
        return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值