hdu——2586 How far away ?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu——2586 How far away ?相关的知识,希望对你有一定的参考价值。

How far away ?

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


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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 3234 2818 
 
 
挨着做的,然后、、、、、
和上一题一样的水题、、、(模板题、、、)
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 41000
using namespace std;
int t,n,m,x,y,z,ans,tot;
int fa[N],top[N],size[N],deep[N],head[N],dis[N];
struct Edge
{
    int to,dis,from,next;
}edge[N<<1];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
int begin()
{
    ans=0;tot=0;
    memset(fa,0,sizeof(fa));
    memset(top,0,sizeof(top));
    memset(dis,0,sizeof(dis));
    memset(edge,0,sizeof(edge));
    memset(deep,0,sizeof(deep));
    memset(size,0,sizeof(size));
    memset(head,0,sizeof(head));
}
int dfs(int x)
{
    size[x]=1;
    deep[x]=deep[fa[x]]+1;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]==to) continue;
        dis[to]=dis[x]+edge[i].dis;
        fa[to]=x,dfs(to);
        size[x]+=size[to];
    }
}
int dfs1(int x)
{
    int t=0;
    if(!top[x]) top[x]=x;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&size[t]<size[to]) t=to;
    }
    if(t) top[t]=top[x],dfs1(t);
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&t!=to)
         dfs1(to);
    }
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
      if(deep[top[x]]<deep[top[y]]) swap(x,y);
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
int main()
{
    t=read();
    while(t--)
    {
        n=read();m=read();begin();
        for(int i=1;i<n;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z),add(y,x,z);
        }
        dfs(1),dfs1(1);
        for(int i=1;i<=m;i++)
        {
            x=read(),y=read();
            ans=dis[x]+dis[y]-2*dis[lca(x,y)];
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

以上是关于hdu——2586 How far away ?的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2586 How far away ?

HDU 2586 How far away ?

HDU2586---How far away ?(lca算法)

HDU-2586 How far away?

HDU-2586 How far away?

hdu——2586 How far away ?