ZOJ 3195 Design the city 【LCA】

传送门

Description

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.

 

题意:给你棵无根双向边的树(结点 0 — N-1 ),多次询问三个点 a,b,c 将三个点连起来的最短距离是多少?

思路:我们知道在一棵树上,两个点之间的最短就是两个点到他们LCA的距离之和,那么三个点怎么求?我们多画几个样例图就可以得到答案,我们发现 dis[a][b] ,dis[b][c] ,dis[a][c] 三个距离把我们所需要的答案的边重复了一次,所以答案就很明显了。
dis[a][b] +dis[b][c]+dis[a][c] /2。

附上代码:

///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const double E=2.718281828459;
const ll mod=1e12;
const int INF=0x3f3f3f3f;

int n,q;

struct node
{
    int e;
    int c;
    int p;
}load[100000];
int head[50005],sign;

void add_edge(int s,int e,int c)
{
    load[++sign]=node{e,c,head[s]};
    head[s]=sign;
}

int grand[50005][20],N;
int depth[50005],dis[50005];

void dfs(int s,int pre)
{
    for(int i=1;i<=N;i++)
        grand[s][i]=grand[grand[s][i-1]][i-1];
    for(int i=head[s];i!=-1;i=load[i].p)
    {
        int e=load[i].e;
        int c=load[i].c;
        if(e!=pre)
        {
            depth[e]=depth[s]+1;
            dis[e]=dis[s]+c;
            grand[e][0]=s;
            dfs(e,s);
        }
    }
}

int get_lca(int a,int b)
{
    if(depth[a]>depth[b])
        swap(a,b);
    for(int i=N;i>=0;i--)
        if(depth[b]>=depth[a]&&depth[grand[b][i]]>=depth[a])
            b=grand[b][i];
    for(int i=N;i>=0;i--)
    {
        if(grand[a][i]!=grand[b][i])
        {
            a=grand[a][i];
            b=grand[b][i];
        }
    }
    return a==b?a:grand[b][0];
}

void init()
{
    sign=0;
    N=log2(n);
    memset(grand,0,sizeof(grand));
    for(int i=1;i<=n;i++)
    {
        depth[i]=dis[i]=0;
        head[i]=-1;
    }
    depth[0]=-1;
}

int main()
{
    int s,e,c,t=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(t++)
            printf("\n");
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d %d %d",&s,&e,&c);
            s++;
            e++;
            add_edge(s,e,c);
            add_edge(e,s,c);
        }
        dfs(1,-1);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d %d %d",&s,&e,&c);
            s++,e++,c++;
            int A=get_lca(s,e);
            int x=dis[s]+dis[e]-2*dis[A];
            int B=get_lca(e,c);
            int y=dis[e]+dis[c]-2*dis[B];
            int C=get_lca(s,c);
            int z=dis[s]+dis[c]-2*dis[C];
            printf("%d\n",(x+y+z)/2);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值