HDU 2586——How far away ?——————【LCA模板题】

How far away ?

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


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
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
 
 
Source
 
题目大意:给你一棵树,边的长度,求任意两点间的最短距离。
 
解题思路:直接套LCA模板就行了。LCA练手。
 
 
在线:
vset[]数组含义:dfs过程中记录经过的节点编号,其实下标可以看做是时间
dep[]数组含义:表示节点的在树中的深度
first[]数组含义:dfs过程中第一次到达节点的时间
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
struct AdjEdge{
    int to,w,next;
}adjedges[maxn];
int head[maxn];
int dis[maxn],vset[maxn],dep[maxn],d[maxn][30],first[maxn];
int tot,nn;
void init(){
    tot=0;
    nn=0;
    memset(dep,0,sizeof(dep));
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
    memset(d,0,sizeof(d));
    memset(first,0,sizeof(first));
}
void addedge(int _u,int _v,int _w){     //
    adjedges[tot].to=_v;
    adjedges[tot].w=_w;
    adjedges[tot].next=head[_u];
    head[_u]=tot++;
    adjedges[tot].to=_u;
    adjedges[tot].w=_w;
    adjedges[tot].next=head[_v];
    head[_v]=tot++;
}

void dfs(int _u,int _fa,int _dep){
   // printf("%d %d
",_u,_dep);
    dep[_u]=_dep;
    vset[++nn]=_u;
    first[_u]=nn;
    for(int i=head[_u];i!=-1;i=adjedges[i].next){
        AdjEdge & e = adjedges[i];
        if(e.to!=_fa){
            dis[e.to]=dis[_u]+e.w;
            dfs(e.to,_u,_dep+1);
            vset[++nn]=_u;
        }
    }
}
void ST(){
    for(int i=1;i<=nn;i++)
        d[i][0]=vset[i];
    for(int j=1;(1<<j)<=nn;j++){
        for(int i=1; i+(1<<j)-1<=nn ; i++){
            if(dep[d[i][j-1]]<dep[d[i+(1<<(j-1))][j-1]])
                d[i][j]=d[i][j-1];
            else d[i][j]=d[i+(1 << (j-1))][j-1];
        }
    }
}

int RMQ(int L,int R){
    int k=0;
    while((1<<(k+1))<=R-L+1) k++;
    if(dep[d[L][k]]<=dep[d[R-(1<<k)+1][k]])
        return d[L][k];
    return d[R-(1<<k)+1][k];
}

int main(){
    int T,n,q;
    scanf("%d",&T);
    while(T--){
        init();
        int a,b,c;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
        }
        dfs(1,-1,1);
        ST();
        for(int i=0;i<q;i++){
            scanf("%d%d",&a,&b);
            if(first[a]<=first[b]){
                int tmp1=RMQ(first[a],first[b]);
                printf("%d
",dis[a]+dis[b]-2*dis[tmp1]);
            }else{
                int tmp1=RMQ(first[b],first[a]);
                printf("%d
",dis[a]+dis[b]-2*dis[tmp1]);
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/4840096.html