Hdu 2586 How far away ?【倍增LCA模板记录】

本文介绍了一种求解树状结构中任意两点间最短路径的算法,并提供了详细的实现代码。通过预处理每个节点到其祖先的距离,利用最近公共祖先(LCA)的概念,快速计算两点间的距离。

How far away ?

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


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

题目大意:


求树上两点间最短距离。


Ans=Dist【1】【x】+Dist【1】【y】-2*Dist【1】【Lca(x,y)】;


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 100500
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[150000];
int cont,n,m;
int p[150000][20];
int d[150000];
int dist[150000];
int head[150000];
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dfs(int u,int from)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(v==from)continue;
        d[v]=d[u]+1;
        dist[v]=dist[u]+e[i].w;
        p[v][0]=u;
        Dfs(v,u);
    }
}
void init()
{
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i<=n;i++)
        {
            p[i][j]=p[p[i][j-1]][j-1];
        }
    }
}
int Lca(int x,int y)
{
    if(d[x]>d[y])swap(x,y);
    int f=d[y]-d[x];
    for(int i=0;(1<<i)<=f;i++)
    {
        if((1<<i)&f)y=p[y][i];
    }
    if(x!=y)
    {
        for(int i=(int)log2(N);i>=0;i--)
        {
            if(p[x][i]!=p[y][i])
            {
                x=p[x][i];
                y=p[y][i];
            }
        }
        x=p[x][0];
    }
    return x;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        scanf("%d%d",&n,&m);
        memset(d,0,sizeof(d));
        memset(dist,0,sizeof(dist));
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n-1;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        Dfs(1,-1);
        init();
        while(m--)
        {
            int x,y;scanf("%d%d",&x,&y);
            printf("%d\n",dist[x]+dist[y]-2*dist[Lca(x,y)]);
        }
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值