POJ 1986 Distance Queries(离线LCA)

来源:互联网 发布:淘宝二级页面 编辑:程序博客网 时间:2024/06/02 04:02

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 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 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. 

自然想到:dis(u,v)=dis(u)+dis(v)-2*dis(LCA(u,v));

对于dis来说就是求根节点到某点的距离。LCA即可。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=40100;const int maxq=40100;int pre[maxn];int Find(int x){    if(pre[x]!=x)        pre[x]=Find(pre[x]);    return pre[x];}int Union(int a,int b){    a=Find(a);b=Find(b);    if(a!=b)  pre[b]=a;}bool vis[maxn],f[maxn];struct node{    int v,w;    int next;}e[maxn*2];int head[maxn],cnt;void addedge(int u,int v,int w){    e[cnt].v=v;e[cnt].w=w;    e[cnt].next=head[u];    head[u]=cnt++;}struct query{    int u,v,next;    int id,w;}q[maxq*2];int h[maxq],tt;int dis[maxn];void add_query(int u,int v,int i){    q[tt].u=u;q[tt].v=v;q[tt].next=h[u];    q[tt].id=i;h[u]=tt++;    q[tt].u=v;q[tt].v=u;q[tt].next=h[v];    q[tt].id=i;h[v]=tt++;}int n,m;void init(){    cnt=tt=0;    CLEAR(head,-1);CLEAR(h,-1);CLEAR(dis,0);    CLEAR(vis,false);CLEAR(f,0);    REPF(i,1,n)  pre[i]=i;}void LCA(int u){    f[u]=u;vis[u]=true;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].v;        if(vis[v])  continue;        dis[v]=dis[u]+e[i].w;        LCA(v);Union(u,v);        f[Find(u)]=u;    }    for(int i=h[u];i!=-1;i=q[i].next)    {        int v=q[i].v;        if(vis[v])            q[i^1].w=q[i].w=dis[v]+dis[u]-2*dis[Find(v)];    }}int main(){    int x,y,w,k,root;    char str[5];    while(~scanf("%d%d",&n,&m))    {        init();        REPF(i,1,m)        {            scanf("%d%d%d%s",&x,&y,&w,str);            addedge(x,y,w);            addedge(y,x,w);        }        scanf("%d",&k);        REP(i,k)        {            scanf("%d%d",&x,&y);            add_query(x,y,i);        }        LCA(1);        for(int i=0;i<k*2;i+=2)           printf("%d\n",q[i].w);    }    return 0;}


0 0
原创粉丝点击