zoj 3195 Design the city LCA
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.
In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.
Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.
Input
The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.
Process to the end of file.
Output
Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.
Output a blank line between each test cases.
Sample Input
4 0 1 1 0 2 1 0 3 1 2 1 2 3 0 1 2 5 0 1 1 0 2 1 1 3 1 1 4 1 2 0 1 2 1 0 3
Sample Output
3 2 2 2
LCA 入门
-------------------
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int INF=0x3f3f3f; const int maxn=111111; const int maxm=111111; int n,m; struct EDGENODE{ int to; int w; int next; }; struct SGRAPH{ int head[maxn]; EDGENODE edges[maxm]; int edge; void init(){ memset(head,-1,sizeof(head)); edge=0; } void addedge(int u,int v,int c){ edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++; } //------------ int d[maxn][20]; //元素从1编号到n void makeRmqIndex(int A[],int n){ for(int i=1;i<=n;i++) d[i][0]=i; for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) d[i][j] = A[d[i][j-1]] < A[d[i+(1<<(j-1))][j-1]]? d[i][j-1]:d[i+(1<<(j-1))][j-1]; } int rmqIndex(int L,int R,int A[]) { int k=0; while ((1<<(k+1))<=R-L+1) k++; return A[d[L][k]]<A[d[R-(1<<k)+1][k]]? d[L][k]:d[R-(1<<k)+1][k]; } //--------------------- int E[maxn*2],R[maxn],D[maxn*2],mn; void dfs(int u,int p,int d){ E[++mn]=u; D[mn]=d; R[u]=mn; for (int i=head[u];i!=-1;i=edges[i].next){ int v=edges[i].to; if (v==p) continue; dfs(v,u,d+1); E[++mn]=u; D[mn]=d; } } void LCA_init(){ mn=0; memset(R,0,sizeof(R)); dfs(1,-1,1); makeRmqIndex(D,mn); getd(1,-1,0); } int LCA(int u,int v){ if (R[u]>=R[v]) return E[rmqIndex(R[v],R[u],D)]; else return E[rmqIndex(R[u],R[v],D)]; } //-------------------- int deep[maxn]; void getd(int u,int p,int w){ deep[u]=w; for (int i=head[u];i!=-1;i=edges[i].next){ int v=edges[i].to; if (v==p) continue; getd(v,u,w+edges[i].w); } } int getDis(int u,int v){ int lca=LCA(u,v); return deep[u]+deep[v]-deep[lca]*2; } int done(int x,int y,int z){ int ans=INF,res=0; int lca1,lca2; lca1=LCA(x,y); res=deep[x]+deep[y]-deep[lca1]*2; lca2=LCA(lca1,z); res+=deep[lca1]+deep[z]-deep[lca2]*2; ans=min(ans,res); lca1=LCA(x,z); res=deep[x]+deep[z]-deep[lca1]*2; lca2=LCA(lca1,y); res+=deep[lca1]+deep[y]-deep[lca2]*2; ans=min(ans,res); lca1=LCA(y,z); res=deep[y]+deep[z]-deep[lca1]*2; lca2=LCA(lca1,x); res+=deep[lca1]+deep[x]-deep[lca2]*2; ans=min(ans,res); return ans; } }solver; int main() { int cas=0; while (~scanf("%d",&n)) { if (cas) puts(""); cas++; solver.init(); for (int i=0;i<n-1;i++) { int x,y,c; scanf("%d%d%d",&x,&y,&c); x++; y++; solver.addedge(x,y,c); solver.addedge(y,x,c); } solver.LCA_init(); scanf("%d",&m); int x,y,z; while (m--) { scanf("%d%d%d",&x,&y,&z); x++;y++;z++; printf("%d\n",solver.done(x,y,z)); } } return 0; }