hdu2586How far away ?

本文介绍了一道关于求解两个节点间的最短路径的问题,并提供了一个详细的解决方案。该方案使用了LCA算法来找到最短路径,适用于村庄中有多个房子且存在唯一简单路径的情况。

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19879    Accepted Submission(s): 7786


Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 

Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 

Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
 

Sample Output
10 25 100 100
思路:LCA模板题
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=100010;
const int N=100010;
int head[N],h[N],dis[N],f[N],a[N];
bool in[N],vis[N];
int cnt,num,tt;
string s1,s2;
int n,m;
map<string,int>mp;
struct node
{
    int u,v,w,next;
}e[M<<1],q[M<<1];
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)
{
     q[tt].v=v;
     q[tt].w=w;
     q[tt].next=h[u];
    h[u]=tt++;
    q[tt].v=u;
     q[tt].w=w;
     q[tt].next=h[v];
    h[v]=tt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(h,-1,sizeof(h));
    memset(in,false,sizeof(in));
    memset(vis,false,sizeof(vis));
    memset(f,-1,sizeof(f));
    memset(dis,0,sizeof(dis));
    cnt=num=tt=0;
}
int findx(int x)
{
    return f[x]==-1?x:f[x]=findx(f[x]);
}
void unio(int x,int y)
{
    int xx=findx(x);
    int yy=findx(y);
    if(yy!=xx)f[yy]=xx;

}
void tarjan(int u)
{
    int i,v;
    for(i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].v;
        if(vis[v])continue;
        dis[v]=dis[u]+e[i].w;
        tarjan(v);
        unio(u,v);
    }
    vis[u]=true;
    for(i=h[u];i!=-1;i=q[i].next)
    {
        v=q[i].v;
        if(vis[v])
        {
            a[q[i].w]=dis[u]-dis[findx(u)]+dis[v]-dis[findx(v)];
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        int i;
        int u,v,w;
        for(i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            in[v]=true;
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            addque(u,v,i);
        }
        for(i=1;i<=n;i++)
        {
            if(!in[i])
            {
                tarjan(i);
                break;
            }
        }
        for(i=0;i<m;i++)
        {
            printf("%d\n",a[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值