码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2586 How far away?

时间:2015-08-06 00:33:07      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

A - How far away ?
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 

这是我人生中见到的最草泥马的题目,g++ RE了几十遍,c++ 交a了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define maxn 40005
#define maxe 205
int fa[maxn],dis[maxn],ancestor[maxn];
bool vis[maxn];
int first[maxn],nxt[maxn<<1],to[maxn<<1],cost[maxn<<1],e;
int qfirst[maxn],qnxt[maxe<<1],qto[maxe<<1],qpre[maxe<<1],qe;
int n,m;
void add(int u,int v,int c){
    to[e]=v;cost[e]=c;nxt[e]=first[u];first[u]=e++;
}
void qadd(int u,int v){
    qto[qe]=v;qpre[qe]=u;qnxt[qe]=qfirst[u];qfirst[u]=qe++;
}

void inital(){
    e=0;
    qe=0;
    dis[1]=0;
    for(int i=0;i<=n;i++){
        fa[i]=i;
        vis[i]=0;
        first[i]=-1;
        qfirst[i]=-1;
    }
}
int finds(int x){
    if(fa[x]!=x)return fa[x]=finds(fa[x]);
}
void unions(int x,int y){
    int a=finds(x);
    int b=finds(y);
    if(a!=b)fa[b]=a;
}
void lca(int u,int fa){
    for(int i=first[u];~i;i=nxt[i]){
        int v=to[i];int c=cost[i];
        if(v==fa)continue;
        if(!vis[v]){
            dis[v]=dis[u]+c;
            lca(v,u);
            unions(u,v);
       }
    }
    vis[u]=true;
    for(int i=qfirst[u];~i;i=qnxt[i]){
        int qv=qto[i];
        if(vis[qv]){
            ancestor[i]=ancestor[i^1]=finds(qv);
        }
    }
}
int main()
{
    int t,u,v,c;
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        inital();
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&u,&v,&c);
                add(u,v,c);
                add(v,u,c);
        }

        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            qadd(u,v);
            qadd(v,u);
        }
        lca(1,-1);
        for(int i=0;i<m;i++){
            int root=ancestor[i<<1];
            int u=qpre[i<<1];
            int v=qto[i<<1];
            printf("%d\n",dis[u]+dis[v]-2*dis[root]);
        }
    }
}


1

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 2586 How far away?

标签:

原文地址:http://blog.csdn.net/u013497977/article/details/47306075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!