HDU 2586 How far away ?(map+lca【暴力水】)

来源:互联网 发布:淘宝内部优惠券怎么弄 编辑:程序博客网 时间:2024/06/08 09:38

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586

How far away ?

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

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
ECJTU 2009 Spring Contest

【中文题意】有t组数据,每组数据一个n,一个m,n代表点的个数,m代表询问的次数。
下面n-1行每行输入三个整数i,j,k,意思为,从i到j有一条长度为k的双向边。
下面m行每行两个整数u,v;让你输出从u到v的最短距离。
【思路分析】运用lca的思想,以1为树根DFS建立树,记录每个结点的深度和父结点。
用map记录彼此之间的距离。
求的时候直接用暴力求LCA的方法求出距离即可【貌似没有向我这么做的】。
【AC代码】

#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<stack>#include<map>#include<algorithm>using namespace std;int t,n,q;vector<int>G[50005];#define root 1int parent[50005];int depth[50005];map<int,map<int,int> >m;void dfs(int v,int p,int d){    parent[v]=p;    depth[v]=d;    for(int i=0; i<G[v].size(); i++)    {        if(G[v][i]!=p)        {            dfs(G[v][i],v,d+1);        }    }}void init(){    dfs(root,-1,0);}int lca(int u,int v){    int sum=0;    while(depth[u]>depth[v])    {        sum+=m[u][parent[u]];        u=parent[u];    }    while(depth[v]>depth[u])    {        sum+=m[v][parent[v]];        v=parent[v];    }    while(u!=v)    {        sum+=m[v][parent[v]];        sum+=m[u][parent[u]];        u=parent[u];        v=parent[v];    }    return sum;}int main(){    scanf("%d",&t);    while(t--)    {        m.clear();        for(int i=0;i<n;i++)        {            G[i].clear();        }        scanf("%d%d",&n,&q);        int u,v,cost;        for(int i=0; i<n-1; i++)        {            scanf("%d%d%d",&u,&v,&cost);            G[u].push_back(v);            G[v].push_back(u);            m[u][v]=cost;            m[v][u]=cost;            //printf("%d**\n",m[u][v]);        }        init();        for(int i=1; i<=q; i++)        {            scanf("%d%d",&u,&v);            printf("%d\n",lca(u,v));        }    }    return 0;}
0 0
原创粉丝点击