How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14685 Accepted Submission(s): 5554
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.
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
题意:给你一颗带权树 求任意两个结点间的最短距离
题解:dis存结点到根的距离+lca dis=dis[a]+dis[b]-2*dis[lca(a,b)]; 倍增法求lca
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <map> 10 #define ll __int64 11 #define mod 1000000007 12 #define dazhi 2147483647 13 #define bug() printf("!!!!!!!") 14 using namespace std; 15 #define maxn 40010 16 #define M 22 17 struct node 18 { 19 int pre; 20 int to; 21 int w; 22 }N[maxn*2]; 23 int pre[maxn]; 24 int deep[maxn],nedge=0; 25 int dis[maxn]; 26 int rudu[maxn]; 27 int fa[maxn][M]; 28 int t; 29 int f1,t1,w1; 30 int l,r; 31 int n,m; 32 void add(int from ,int to,int ww) 33 { 34 nedge++; 35 N[nedge].to=to; 36 N[nedge].pre=pre[from]; 37 N[nedge].w=ww; 38 pre[from]=nedge; 39 } 40 void dfs(int u) 41 { 42 for(int i=pre[u];i;i=N[i].pre) 43 { 44 int v=N[i].to; 45 if(deep[v]==0) 46 { 47 dis[v]=dis[u]+N[i].w; 48 deep[v]=deep[u]+1; 49 fa[v][0]=u; 50 dfs(v); 51 } 52 } 53 } 54 void st(int n) 55 { 56 for(int j=1;j<M;j++) 57 for(int i=1;i<=n;i++) 58 fa[i][j]=fa[fa[i][j-1]][j-1]; 59 } 60 int lca(int u,int v) 61 { 62 if(deep[u]<deep[v]) swap(u,v); 63 int d=deep[u]-deep[v]; 64 int i; 65 for(i=0;i<M;i++) 66 { 67 if((1<<i)&d) 68 { 69 u=fa[u][i]; 70 } 71 } 72 if(u==v) return u; 73 for(i=M-1;i>=0;i--) 74 { 75 if(fa[u][i]!=fa[v][i]) 76 { 77 u=fa[u][i]; 78 v=fa[v][i]; 79 } 80 } 81 u=fa[u][0]; 82 return u; 83 } 84 void init() 85 { 86 memset(rudu,0,sizeof(rudu)); 87 memset(dis,0,sizeof(dis)); 88 memset(deep,0,sizeof(deep)); 89 memset(fa,0,sizeof(fa)); 90 memset(N,0,sizeof(N)); 91 memset(pre,0,sizeof(pre)); 92 nedge=0; 93 } 94 int main() 95 { 96 while(scanf("%d",&t)!=EOF) 97 { 98 for(int i=1;i<=t;i++) 99 { 100 init(); 101 scanf("%d %d",&n,&m); 102 for(int j=1;j<n;j++) 103 { 104 scanf("%d %d %d",&f1,&t1,&w1); 105 add(f1,t1,w1); 106 rudu[t1]++; 107 } 108 for(int j=1;j<=n;j++) 109 { 110 if(rudu[j]==0) 111 { 112 deep[j]=1; 113 dis[j]=0; 114 dfs(j); 115 break; 116 } 117 } 118 st(n); 119 for(int j=1;j<=m;j++) 120 { 121 int aa,bb; 122 scanf("%d %d",&aa,&bb); 123 printf("%d\n",dis[aa]+dis[bb]-2*dis[lca(aa,bb)]); 124 } 125 } 126 } 127 return 0; 128 }