Distance Queries
Description
Farmer John ' s cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare", followed by a line containing a single I Nteger K, followed by K "Distance Queries". Each distance query was a line of input containing and integers, giving the numbers of the of the farms between which FJ is Intere Sted in computing distance (measured in the length of the roads along the path between the farms). Please answer FJ ' s distance queries as quickly as possible!
Input
* Lines 1..1+m:same format as "Navigation Nightmare"
* Line 2+m:a single integer, K. 1 <= k <= 10,000
* Lines 3+m. 2+m+k:each line corresponds to a distance query and contains the indices of both farms.
Output
* Lines 1..k:for Each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
7 6 E6 3 9 E3 5 7 S4 1 3 N2 4 W4 7 2 S31 61 42 6
Sample Output
13336
Hint
Farms 2 and 6 are 20+3+13=36 apart. Template questions
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>#defineTrue Ture#defineFalse flaseusing namespacestd;#definell Long Long#defineINF 0XFFFFFFFintScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}#defineMAXN 100010#defineM 22struct is{ intV,next,w;} EDGE[MAXN*2];intDeep[maxn],jiedge;intDIS[MAXN];intHEAD[MAXN];intRUDU[MAXN];intFa[maxn][m];voidAddintUintVintW) {Jiedge++; EDGE[JIEDGE].V=v; EDGE[JIEDGE].W=W; Edge[jiedge].next=Head[u]; Head[u]=Jiedge;}voidDfsintu) { for(intI=head[u]; I I=Edge[i].next) { intv=edge[i].v; intw=EDGE[I].W; if(!Deep[v]) {Dis[v]=dis[u]+EDGE[I].W; DEEP[V]=deep[u]+1; fa[v][0]=u; DFS (v); } }}voidStintN) { for(intj=1; j<m; J + +) for(intI=1; i<=n; i++) Fa[i][j]=fa[fa[i][j-1]][j-1];}intLCA (intU,intv) { if(Deep[u] <Deep[v]) Swap (U, v); intD = Deep[u]-Deep[v]; inti; for(i =0; i < M; i + +) { if( (1<< i) & D)//note here, try to simulate, you will understand{u=Fa[u][i]; } } if(U = = v)returnu; for(i = M-1; I >=0; I--) { if(Fa[u][i]! =Fa[v][i]) {u=Fa[u][i]; V=Fa[v][i]; }} u= fa[u][0] ; returnu;}voidinit () {memset (head,0,sizeof(head)); memset (FA,0,sizeof(FA)); memset (Rudu,0,sizeof(Rudu)); memset (Deep,0,sizeof(deep)); Jiedge=0;}intMain () {intx,n,t; while(~SCANF ("%d%d",&n,&x)) {init (); for(intI=0; i<x; i++) { Chara[2]; intu,v,w; scanf ("%d%d%d%s",&u,&v,&w,a); Add (U,V,W); Add (v,u,w);//bidirectional can start at any point and avoid a loop} deep[1]=1; dis[1]=0; DFS (1); St (n); scanf ("%d",&t); while(t--) { intb; scanf ("%d%d",&a,&b); printf ("%d\n", dis[a]-2*dis[lca (A, b)]+Dis[b]); } } return 0;}
POJ 1986 Distance Queries Weighted LCA template question