hdu 2586 How far away ?(倍增法LCA)

来源:互联网 发布:装饰器模式 java 编辑:程序博客网 时间:2024/05/20 21:57

How far away ?

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


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条路把它们链接起来,给出每条路的距离,有k个问题,询问两个房间的最小距离。


分析:n个点,n-1条边,组成了一颗树,所以 这是求树上两结点距离的问题,试了一手自己的倍增法求LCA模板。

之前有一篇写过倍增法求LCA 的详解  点击打开链接


AC代码:

#include<stdio.h>#include<string.h>#include<vector>#include<math.h>using namespace std;struct tree{int from,to,w;tree(int ff,int tt,int ww){from=ff;to=tt;w=ww;}tree(){}};vector<tree>G;vector<int>V[400200];int mxdeep;int father[50050][30];   //记录祖先结点编号 int deep[50050];        //记录结点深度int dis[50050][30];   //记录结点和祖先结点的距离 void addtree(int from,int to,int w) //建边 {G.push_back(tree(from,to,w));G.push_back(tree(to,from,w));int e=G.size();V[from].push_back(e-2);V[to].push_back(e-1);} void init(int n){memset(father,0,sizeof(father));memset(deep,0,sizeof(deep));memset(dis,0,sizeof(dis));mxdeep=log(n*1.0)/log(2.0);G.clear();for(int i=0;i<=400000;i++)V[i].clear();    }void dfs(int root){for(int i=1;i<=mxdeep;i++){father[root][i]=father[father[root][i-1]][i-1];dis[root][i]=dis[father[root][i-1]][i-1]+dis[root][i-1];if(!father[root][i])   //优化,当为0的时候说明已经更新到了根结点位置 break;}for(int i=0;i<V[root].size();i++){tree &e=G[V[root][i]];if(e.to!=father[root][0]) //说明e.to是root的儿子{deep[e.to]=deep[root]+1;father[e.to][0]=root;  //建立父子关系dis[e.to][0]=e.w;      //更新到父亲的距离dfs(e.to); } } }int lca(int u,int v){int ans=0;if(deep[u]>deep[v])  //让u结点不在v结点下方,方便操作 swap(u,v);for(int i=mxdeep;i>=0;i--)  //将 v结点上移,但不能高于u结点 {if(deep[u]<deep[v]&&deep[u]<=deep[father[v][i]]){//printf("! %d %d %d %d\n",u,v,i,father[v][i]);ans+=dis[v][i];v=father[v][i];}}if(v==u)return ans;for(int i=mxdeep;i>=0;i--){if(father[v][i]!=father[u][i])  //一起移动到共同父亲处 {ans+=dis[v][i]+dis[u][i];v=father[v][i];u=father[u][i];}}if(v!=u) ans+=dis[v][0]+dis[u][0];  // 如果没有移动到同一处,可能是   1->u   1->v ,那么向上移动一个位置就可以了 return ans;}int main(){int T;scanf("%d",&T);while(T--){int n,k;scanf("%d%d",&n,&k);init(n);int root;for(int i=0;i<n-1;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);father[v][0]=u;dis[v][0]=w;addtree(u,v,w);if(father[u][0]==0)root=u;}deep[root]=1;dfs(root);  //建树 for(int i=0;i<k;i++){int u,v;scanf("%d%d",&u,&v);printf("%d\n",lca(u,v));}}}


原创粉丝点击