POJ 1986——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

Hint

Farms 2 and 6 are 20+3+13=36 apart.

Source

USACO 2004 February


给一张图,其实是一棵无根树,问任意两点的最近距离,那么我们可以用个数组记录每个点到根的距离(任意找个点作为根),而在求距离的时候,势必经过他们的LCA,所以,问题就转化为求LCA了,而最后2点的距离就是dis(u,root)-LCA+dis(v,root)-LCA.
vector做法:1266ms
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn=100005;
vector<pair<int,int> >tree[maxn],que[maxn];//树和询问
int ans[maxn],dis[maxn];
bool vis[maxn];
int father[maxn];
int ancestor[maxn];
int rank[maxn];
int in[maxn];//入度,找根
int n;

void init()
{
    for (int i=1;i<=n;i++)
    {
        rank[i]=1;
        father[i]=i;
        in[i]=0;
        vis[i]=0;
        ancestor[i]=0;
        ans[i]=0;
        //dis[i]=0;
        que[i].clear();
        tree[i].clear();
    }
}

int find(int x)
{
    if (x==father[x])
        return x;
    father[x]=find(father[x]);
    return father[x];
}

void Union(int x,int y)
{
    int a=find(x);
    int b=find(y);
    if (a!=b)
    {
        if (rank[a]<=rank[b]) //合并的时候按深度浅的向深的合并
        {
            father[a]=b;
            rank[b]+=rank[a];
        }
        else
        {
            father[b]=a;
            rank[a]+=rank[b];
        }
    }
}

void LCA(int root,int fa)
{
    ancestor[root]=root;
    int size=tree[root].size();
    for (int i=0;i<size;i++)
    {
    	if(tree[root][i].first==fa)
    	  continue;
    	dis[tree[root][i].first]=dis[root]+tree[root][i].second;
        LCA(tree[root][i].first,root);
        Union(root,tree[root][i].first);
        ancestor[find(root)]=root;
    }
    vis[root]=1;
    size=que[root].size();
    for (int i=0;i<size;i++)
    {
        if (vis[que[root][i].first])
        	ans[que[root][i].second]=dis[root]+dis[que[root][i].first]-2*dis[ancestor[find(que[root][i].first)]];
    }
}

int main()
{
    int m;
    //scanf("%d",&t);
   // while (t--)
   while(~scanf("%d%d",&n,&m))
    {
        int x,y,d,k;
        char c[4];
        //scanf("%d",&n);
        init();
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d%d%s",&x,&y,&d,c);
            tree[x].push_back(make_pair(y,d));
            tree[y].push_back(make_pair(x,d));
           // in[y]++;
        }
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
        	scanf("%d%d",&x,&y);
        	que[x].push_back(make_pair(y,i));
            que[y].push_back(make_pair(x,i));
        }
        dis[1]=0;
       // scanf("%d%d",&x,&y);
       LCA(1,0);
        for (int i=1;i<=k;i++)
        {
        	printf("%d\n",ans[i]);
        }
    }
    return 0;
}

前向星:297ms
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn=100005;
vector<pair<int,int> >que[maxn];
int ans[maxn],dis[maxn];
bool vis[maxn];
int father[maxn];
int ancestor[maxn];
int rank[maxn];
int in[maxn];//入度,找根
int n;

struct node
{
	int weight;
	int to;
	int next;
}edge[maxn];
int head[maxn];
int tot;

void addedge(int from,int to,int weight)
{
	edge[tot].to=to;
	edge[tot].weight=weight;
	edge[tot].next=head[from];
	head[from]=tot++;
}

void init()
{
	memset(head,-1,sizeof(head));
	tot=0;
    for (int i=1;i<=n;i++)
    {
        rank[i]=1;
        father[i]=i;
        in[i]=0;
        vis[i]=0;
        ancestor[i]=0;
        ans[i]=0;
        //dis[i]=0;
        que[i].clear();
        //tree[i].clear();
    }
}

int find(int x)
{
    if (x==father[x])
        return x;
    father[x]=find(father[x]);
    return father[x];
}

void Union(int x,int y)
{
    int a=find(x);
    int b=find(y);
    if (a!=b)
    {
        if (rank[a]<=rank[b]) //合并的时候按深度浅的向深的合并
        {
            father[a]=b;
            rank[b]+=rank[a];
        }
        else
        {
            father[b]=a;
            rank[a]+=rank[b];
        }
    }
}

void LCA(int root,int fa)
{
    ancestor[root]=root;
    //int size=tree[root].size();
    /*for (int i=0;i<size;i++)
    {
    	if(tree[root][i].first==fa)
    	  continue;
    	dis[tree[root][i].first]=dis[root]+tree[root][i].second;
        LCA(tree[root][i].first,root);
        Union(root,tree[root][i].first);
        ancestor[find(root)]=root;
    }*/
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
    	if(edge[i].to==fa)
    	  continue;
  	    dis[edge[i].to]=dis[root]+edge[i].weight;
  	    LCA(edge[i].to,root);
        Union(root,edge[i].to);
        ancestor[find(root)]=root;
    }
    vis[root]=1;
    int size=que[root].size();
    for (int i=0;i<size;i++)
    {
        if (vis[que[root][i].first])
        	ans[que[root][i].second]=dis[root]+dis[que[root][i].first]-2*dis[ancestor[find(que[root][i].first)]];
    }
}

int main()
{
    int m;
    //scanf("%d",&t);
   // while (t--)
   while(~scanf("%d%d",&n,&m))
    {
        int x,y,d,k;
        char c[4];
        //scanf("%d",&n);
        init();
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d%d%s",&x,&y,&d,c);
            //tree[x].push_back(make_pair(y,d));
            //tree[y].push_back(make_pair(x,d));
            addedge(x,y,d);
            addedge(y,x,d);
           // in[y]++;
        }
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
        	scanf("%d%d",&x,&y);
        	que[x].push_back(make_pair(y,i));
            que[y].push_back(make_pair(x,i));
        }
        dis[1]=0;
       // scanf("%d%d",&x,&y);
       LCA(1,0);
        for (int i=1;i<=k;i++)
        {
        	printf("%d\n",ans[i]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值