zoukankan      html  css  js  c++  java
  • 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): 17350    Accepted Submission(s): 6696


    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 
    LCA的大水题。
    #include<iostream>
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MAXN 500000+15
    using namespace std;
    int T,n,m,tot;
    int dad[MAXN*4],deep[MAXN*4],size[MAXN*4],top[MAXN*4],length[MAXN*4];
    int to[MAXN*5],from[MAXN*5],net[MAXN*5],cap[MAXN*5];
    void add(int u,int v,int w){
        to[++tot]=v;net[tot]=from[u];from[u]=tot;cap[tot]=w;
        to[++tot]=u;net[tot]=from[v];from[v]=tot;cap[tot]=w;
    }
    void dfs(int x){
        size[x]=1;
        deep[x]=deep[dad[x]]+1;
        for(int i=from[x];i;i=net[i])
            if(dad[x]!=to[i]){
                dad[to[i]]=x;
                length[to[i]]=length[x]+cap[i];
                dfs(to[i]);
                size[x]+=size[to[i]];
            }
    }
    void dfs1(int x){
        int t=0;
        if(!top[x])    top[x]=x;
        for(int i=from[x];i;i=net[i])
            if(dad[x]!=to[i]&&size[t]<size[to[i]])
                t=to[i];
        if(t){
            top[t]=top[x];
            dfs1(t);
        }
        for(int i=from[x];i;i=net[i])
            if(dad[x]!=to[i]&&t!=to[i])
                dfs1(to[i]);
    }
    int lca(int x,int y){
        for(;top[x]!=top[y];){
            if(deep[top[x]]<deep[top[y]])
                swap(x,y);
            x=dad[top[x]];
        }
        if(deep[x]>deep[y])
            swap(x,y);
        return x;
    }
    int main(){
        cin>>T;
        while(T--){
            tot=0; 
            memset(to,0,sizeof(to));
            memset(cap,0,sizeof(cap));
            memset(net,0,sizeof(net));
            memset(top,0,sizeof(top));
            memset(dad,0,sizeof(dad));
            memset(from,0,sizeof(from));
            memset(size,0,sizeof(size));
            memset(deep,0,sizeof(deep));
            memset(length,0,sizeof(length));
            scanf("%d%d",&n,&m);
            for(int i=1;i<n;i++){
                int u,v,z;
                scanf("%d%d%d",&u,&v,&z);
                add(u,v,z);
            }
            dfs(1);
            dfs1(1);
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                int l=lca(u,v);
                cout<<length[u]+length[v]-2*length[l]<<endl;
            }
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    Local minimum and local maximum,second derivative
    Analysis by Its History_exercise 1.5
    陶哲轩实分析 例1.22
    域上多项式的带余除法
    Java反射机制
    Java多线程发展简史
    关于Java性能的9个谬论
    Lucene 工作原理
    Java 理论和实践: 了解泛型
    Java Collection
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7399410.html
Copyright © 2011-2022 走看看