zoj 3195 Design the city LCA

文章探讨了城市交通拥堵问题的原因,特别是道路分布设计不佳的影响,并提出了通过优化设计连接城市区域的道路网络来解决这一问题的方法。具体地,文章描述了一个算法,用于计算在给定的循环图形中连接三个特定区域的最短路径长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

-----------------

LCA 入门

-------------------

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int INF=0x3f3f3f;
const int maxn=111111;
const int maxm=111111;
int n,m;

struct EDGENODE{
    int to;
    int w;
    int next;
};
struct SGRAPH{
    int head[maxn];
    EDGENODE edges[maxm];
    int edge;
    void init(){
        memset(head,-1,sizeof(head));
        edge=0;
    }
    void addedge(int u,int v,int c){
        edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
    }
    //------------
    int d[maxn][20];
    //元素从1编号到n
    void makeRmqIndex(int A[],int n){
        for(int i=1;i<=n;i++) d[i][0]=i;
        for(int j=1;(1<<j)<=n;j++)
            for(int i=1;i+(1<<j)-1<=n;i++)
                d[i][j] = A[d[i][j-1]] < A[d[i+(1<<(j-1))][j-1]]? d[i][j-1]:d[i+(1<<(j-1))][j-1];
    }
    int rmqIndex(int L,int R,int A[])
    {
        int k=0;
        while ((1<<(k+1))<=R-L+1) k++;
        return A[d[L][k]]<A[d[R-(1<<k)+1][k]]? d[L][k]:d[R-(1<<k)+1][k];
    }
    //---------------------
    int E[maxn*2],R[maxn],D[maxn*2],mn;
    void dfs(int u,int p,int d){
        E[++mn]=u;
        D[mn]=d;
        R[u]=mn;
        for (int i=head[u];i!=-1;i=edges[i].next){
            int v=edges[i].to;
            if (v==p) continue;
            dfs(v,u,d+1);
            E[++mn]=u;
            D[mn]=d;
        }
    }
    void LCA_init(){
        mn=0;
        memset(R,0,sizeof(R));
        dfs(1,-1,1);
        makeRmqIndex(D,mn);
        getd(1,-1,0);
    }
    int LCA(int u,int v){
        if (R[u]>=R[v]) return E[rmqIndex(R[v],R[u],D)];
        else return E[rmqIndex(R[u],R[v],D)];

    }
    //--------------------
    int deep[maxn];
    void getd(int u,int p,int w){
        deep[u]=w;
        for (int i=head[u];i!=-1;i=edges[i].next){
            int v=edges[i].to;
            if (v==p) continue;
            getd(v,u,w+edges[i].w);
        }
    }
    int getDis(int u,int v){
        int lca=LCA(u,v);
        return deep[u]+deep[v]-deep[lca]*2;
    }
    int done(int x,int y,int z){
        int ans=INF,res=0;
        int lca1,lca2;

        lca1=LCA(x,y);
        res=deep[x]+deep[y]-deep[lca1]*2;
        lca2=LCA(lca1,z);
        res+=deep[lca1]+deep[z]-deep[lca2]*2;
        ans=min(ans,res);

        lca1=LCA(x,z);
        res=deep[x]+deep[z]-deep[lca1]*2;
        lca2=LCA(lca1,y);
        res+=deep[lca1]+deep[y]-deep[lca2]*2;
        ans=min(ans,res);

        lca1=LCA(y,z);
        res=deep[y]+deep[z]-deep[lca1]*2;
        lca2=LCA(lca1,x);
        res+=deep[lca1]+deep[x]-deep[lca2]*2;
        ans=min(ans,res);

        return ans;
    }
}solver;

int main()
{
    int cas=0;
    while (~scanf("%d",&n))
    {
        if (cas) puts("");
        cas++;
        solver.init();
        for (int i=0;i<n-1;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            x++;
            y++;
            solver.addedge(x,y,c);
            solver.addedge(y,x,c);
        }
        solver.LCA_init();
        scanf("%d",&m);
        int x,y,z;
        while (m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            x++;y++;z++;
            printf("%d\n",solver.done(x,y,z));
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值