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;
}

 

全部评论

相关推荐

双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
10-25 22:20
门头沟学院 Java
代码飞升:同学院本,个人亮点去了,打招呼里面的废话也去了,学院本就是路边一条,明天拉满然后该学还是学,小厂也行尽量先有一段实习。另外你的项目描述写的不好,具体列一下可被提问的点,然后量化一下指标或者收益吧
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务