Poj1986 distance queries (LCA)

Source: Internet
Author: User

Portal:

Http://poj.org/problem? Id = 1986

Distance queries
Time limit:2000 ms   Memory limit:30000 K
     
Case time limit:1000 ms

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 integer k, followed by K "distance queries ". each distance query is a line of input containing two I Ntegers, 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 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S31 61 42 6

Sample output

13336

Hint

Farms 2 and 6 are 20 + 3 + 13 = 36 apart.

Source

Usaco 2004 February
 1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 100010;10 #define For(i,n) for(int i=1;i<=n;i++)11 #define Rep(i,l,r) for(int i=l;i<=r;i++)12 13 struct Edge{14     int t,next,w;15 }E[N*2];16 vector< pair<int,int> > Q[N];17 int s,t,w,Es,ans[N],head[N],fa[N],d[N];18 bool vis[N];19 20 int find(int i){21     if(fa[i]==i) return i;22     else         return find(fa[i]);23 }24 25 void makelist(int s,int t,int w){26     E[Es].t = t; E[Es].w = w;E[Es].next = head[s];27     head[s] = Es++;28 }29 30 void LCA(int i,int w){31     d[i] = w;fa[i] = i;32     vis[i] = true;33     for(int p=head[i];p!=-1;p=E[p].next)34         if(!vis[E[p].t]){35             LCA(E[p].t,w+E[p].w);36             fa[E[p].t] = i;37         }38     int  len = Q[i].size();39     for(int j=0;j<len;j++){40         if(vis[Q[i][j].first])41             ans[Q[i][j].second] = w + d[Q[i][j].first] - 2 * d[find(Q[i][j].first)]; 42     }43 }44 int n,m,qn;45 int main(){46     memset(head,-1,sizeof(head));47     scanf("%d%d",&n,&m);48     For(i,m){49         scanf("%d%d%d %*c",&s,&t,&w);50         makelist(s,t,w);makelist(t,s,w);51     }52     scanf("%d",&qn);53     For(i,qn){54         scanf("%d%d",&s,&t);55         Q[s].push_back(make_pair(t,i));Q[t].push_back(make_pair(s,i));56     }57     LCA(1,0);58     For(i,qn) printf("%d\n",ans[i]);59     return 0;60 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.