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
Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009
题目大意:给一个无根树,有q个询问,每个询问3个点,问将这3个点连起来,距离最短是多少
思路:询问3各点之间的最短距离与两个点之间的最短距离相同,我们只需要处理出lca(x,y),lca(y,z), lca(x,z) 然后再求这些点两两之间的最短距离,然后加起来除以2就好了。
鬼畜的输出、、、、
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 51000 using namespace std; int n,m,x,y,z,ans,sum,tot,ans1,ans2,ans3; int fa[N],top[N],size[N],deep[N],head[N],dis[N]; struct Edge { int to,dis,from,next; }edge[N<<1]; int add(int x,int y,int z) { tot++; edge[tot].to=y; edge[tot].dis=z; edge[tot].next=head[x]; head[x]=tot; } int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } int lca(int x,int y) { for(;top[x]!=top[y];x=fa[top[x]]) if(deep[top[x]]<deep[top[y]]) swap(x,y); if(deep[x]>deep[y]) swap(x,y); return x; } int dfs(int x) { size[x]=1; deep[x]=deep[fa[x]]+1; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to) { dis[to]=dis[x]+edge[i].dis; fa[to]=x,dfs(to); size[x]+=size[to]; } } } int dfs1(int x) { int t=0; if(!top[x]) top[x]=x; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&size[t]<size[to]) t=to; } if(t) top[t]=top[x],dfs1(t); for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&to!=t) dfs1(to); } } int begin() { ans=0;tot=0; memset(fa,0,sizeof(fa)); memset(top,0,sizeof(top)); memset(dis,0,sizeof(dis)); memset(edge,0,sizeof(edge)); memset(deep,0,sizeof(deep)); memset(size,0,sizeof(size)); memset(head,0,sizeof(head)); } int main() { while(scanf("%d",&n)!=EOF) { if(sum++) printf("\n"); begin(); for(int i=1;i<n;i++) { x=read(),y=read(),z=read(); add(x,y,z),add(y,x,z); } dfs(0),dfs1(0); m=read(); for(int i=1;i<=m;i++) { x=read(),y=read(),z=read(); ans1=dis[x]+dis[y]-2*dis[lca(x,y)]; ans2=dis[x]+dis[z]-2*dis[lca(x,z)]; ans3=dis[y]+dis[z]-2*dis[lca(y,z)]; ans=(ans1+ans2+ans3)/2; printf("%d\n",ans); } } return 0; }