POJ - 1986 Distance Queries

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 integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two 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 two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart.
 
后面的方向根本没有用,直接构建树模型,用LCA倍增算法求最近公共父亲即可。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 
 7 const int maxn=40005;
 8 const int LOG = 20;
 9 int par[maxn][LOG],dep[maxn];
10 int head[maxn],dis[maxn];
11 int cnt=0;
12 
13 struct EDGE
14 {
15     int v,w,next;
16 }edge[maxn*2];
17 
18 void addedge(int u,int v,int w)
19 {
20     edge[cnt].v=v;
21     edge[cnt].w=w;
22     edge[cnt].next=head[u];
23     head[u]=cnt++;
24 }
25 
26 void dfs(int u,int fa,int depth){
27     dep[u]=depth;
28     if(depth==0)
29     { 
30        for(int i=0;i<LOG;i++) par[u][i]=u;
31     }
32         else
33         {
34             par[u][0]=fa; 
35             for(int i=1;i<LOG;i++) par[u][i]=par[par[u][i-1]][i-1];
36         }
37     for(int i=head[u];i!=-1;i=edge[i].next)
38     {
39         int q=edge[i].v;
40         if(q==fa) continue;
41         dis[q]=dis[u]+edge[i].w;
42         dfs(q,u,depth+1);
43     }
44 }
45 
46 int up(int x,int step){ 
47     for(int i=0;i<LOG;i++) 
48         if(step&(1<<i))
49             x=par[x][i];
50     return x;
51 }
52 
53 int lca(int u,int v){
54     if(dep[u]<dep[v]) 
55         swap(u,v);   
56     u=up(u,dep[u]-dep[v]);      
57     if(u==v) return u;    
58     for(int i=LOG-1;i>=0;i--)
59     {  
60         if(par[u][i]!=par[v][i])
61         {
62             u=par[u][i];
63             v=par[v][i];
64         }
65     }
66     return par[u][0];
67 }
68 
69 int main()
70 {
71     int T,n,m,k;
72     char c;
73     while(~scanf("%d%d",&n,&m))
74     {
75         cnt=0;
76         memset(head,-1,sizeof(head));
77         int x,y,z;
78         for(int i=0;i<m;i++)
79         {
80             scanf("%d%d%d %c",&x,&y,&z,&c);
81             addedge(x,y,z);
82             addedge(y,x,z);
83         }
84         dis[1]=0;
85         dfs(1,-1,0);
86         scanf("%d",&k);
87         for(int i=0;i<k;i++)
88         {
89             scanf("%d%d",&x,&y);
90             int ans=dis[x]+dis[y]-2*dis[lca(x,y)];
91             printf("%d\n",ans);
92         }
93     }
94     
95     return 0;
96 }

 

posted @ 2017-08-24 08:38  西北会法语  阅读(164)  评论(0编辑  收藏  举报