ZOJ Design the city LCA转RMQ

Design the city

Time Limit: 1 Second      Memory Limit: 32768 KB

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

题意: 给你一个无向图,然后M个询问,每次询问给你三个点,问你链接这三个点的最短距离

题解:ST算法,分别求出aa=LCA(a,b),bb=LCA(a,c),cc=LCA(c,b);最短距离就是dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc]);

//1085422276
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 55000
typedef long long ll;
const int inf = (int)1E9+10;
inline ll read()
{
    ll 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 first[maxn],ver[maxn*2];
int R[maxn*2],head[maxn];
int dist[maxn],t,vis[maxn],tot;
struct ss
{
    int from,to,value,next;
} e[maxn*2];
int dp[maxn*2][30];
void init()
{
    t=1;
    memset(head,0,sizeof(head));
}
void add(int u,int v,int w)
{
    ss kk= {u,v,w,head[u]};
    e[t]=kk;
    head[u]=t++;
}
void dfs(int u,int dep)
{
    vis[u]=1;
    ver[++tot]=u;
    first[u]=tot;
    R[tot]=dep;
    for(int i=head[u]; i; i=e[i].next)
    {
        if(!vis[e[i].to])
        {
            int vv=e[i].to;
            dist[vv]=dist[u]+e[i].value;
            dfs(vv,dep+1);
            ver[++tot]=u;
            R[tot]=dep;
        }
    }
}
void find_depth()
{
    tot=0;
    memset(vis,0,sizeof(vis));
    memset(first,0,sizeof(first));///首次出现位置
    memset(ver,0,sizeof(ver));///节点编号
    dfs(1,1);
}
void ST(int n)
{
    for(int i=1; i<=n; i++)dp[i][0]=i;
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<j)-1<=n; i++)
        {
            int a=dp[i][j-1];
            int b=dp[i+(1<<(j-1))][j-1];
            dp[i][j]=R[a]>R[b]?b:a;
        }
    }
}
int RMQ(int x,int y)
{
    int k=0;
    while((1<<(k+1))<=y-x+1)
    {
        k++;
    }
    int a=dp[x][k];
    int b=dp[y-(1<<k)+1][k];
    return R[a]>R[b]?b:a;
}
int LCA(int x,int y)
{
    x=first[x];
    y=first[y];
    if(x>y)swap(x,y);
    return ver[RMQ(x,y)];
}
int main()
{
    int n;
    int a,b,c;
    int T=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(T)cout<<endl;
        init();
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a+1,b+1,c);
            add(b+1,a+1,c);
        }
        dist[1]=0;
        find_depth();
        ST(2*n-1);
        int q=read();
        for(int i=1; i<=q; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            a++;
            b++;
            c++;
            int aa=LCA(a,b);
            int bb=LCA(a,c);
            int cc=LCA(b,c);
            cout<<dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc])<<endl;
        }
        T++;
    }
    return 0;
}
代码
原文地址:https://www.cnblogs.com/zxhl/p/4780421.html