hdu2586(LCA最近公共祖先)

来源:互联网 发布:万网哪些域名可以备案 编辑:程序博客网 时间:2024/05/21 17:07

How far away ?

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


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
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
1025100100
 
题目分析:题目中说有n个房子,有n-1条道路,并且都是联通的,典型的树状图,要求任意两点的距离,可以用线段树做,此处不提,用最短路
n的范围是4000,用一般最短路的方法一定会超时,因此利用树状结构的特点,最好用LCA;
程序:
#include"stdio.h"#include"string.h"#include"iostream"#include"queue"#include"map"using namespace std;#define M 40005int dis[M],pre[M],head[M],t,sum,use[M],rank[M];struct st{    int u,v,w,next;}edge[M*3];void init(){    t=0;    memset(head,-1,sizeof(head));}void add(int u,int v,int w){    edge[t].u=u;    edge[t].v=v;    edge[t].w=w;    edge[t].next=head[u];    head[u]=t++;}void bfs(int s){    queue<int>q;    memset(use,0,sizeof(use));    memset(rank,0,sizeof(rank));    use[s]=1;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v;            if(!use[v])            {                use[v]=1;                pre[v]=u;//记录父节点;                rank[v]=rank[u]+1;//记录层数                dis[v]=edge[i].w;//记录子节点到父节点的距离                q.push(v);            }        }    }}void targan(int a,int b){    if(a==b)    return;    else if(rank[a]>rank[b])    {        sum+=dis[a];        targan(pre[a],b);    }    else    {        sum+=dis[b];        targan(a,pre[b]);    }}//用深搜的方法求最近公共祖先;sum记录路径长度int main(){    int w,a,b,c,i,m,n;    scanf("%d",&w);    while(w--)    {        init();        scanf("%d%d",&n,&m);        for(i=1;i<n;i++)        {            scanf("%d%d%d",&a,&b,&c);            add(a,b,c);            add(b,a,c);        }        bfs(1);        while(m--)        {            scanf("%d%d",&a,&b);            sum=0;            targan(a,b);            printf("%d\n",sum);        }    }}