zoukankan      html  css  js  c++  java
  • ZOJ 3195 Design the city (LCA 模板题)

    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.

    <b< dd="">

    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.

    <b< dd="">

    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
    

    <b< dd="">

    Sample Output

    3
    2
    
    2
    2
    题解:Orz,PE了好几发,格式真的。。。有毒
    题目让求3点间的最短距离;我们用LCA求任意两点间的最短距离,然后将三个结果求和然后除以二即可;画图很容易理解;
    参考代码为:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<stack>
     9 #include<set>
    10 #include<vector>
    11 #include<map>
    12 using namespace std;
    13 typedef long long LL;
    14 const int N=1e5+10,maxn=500+100,inf=0x3f3f3f3f;
    15 int a,b,c;
    16 struct edge{
    17     int to,Next,w;
    18 }e[N];
    19 int head[N],dis[N];
    20 int cnt,depth[N],fa[20][N];
    21 void add(int u,int v,int w)
    22 {
    23     e[cnt].to=v;
    24     e[cnt].w=w;
    25     e[cnt].Next=head[u];
    26     head[u]=cnt++;
    27 }
    28 void dfs(int u,int f)
    29 {
    30     fa[0][u]=f;
    31     for(int i=head[u];~i;i=e[i].Next)
    32     {
    33         int To=e[i].to;
    34         if(To!=f)
    35         {
    36             depth[To]=depth[u]+1;
    37             dis[To]=dis[u]+e[i].w;
    38             dfs(To,u);
    39         }
    40     }
    41 }
    42 void work(int n)
    43 {
    44     depth[1]=1;dis[1]=0;
    45     dfs(1,-1);
    46     for(int i=1;i<20;i++)
    47         for(int j=1;j<=n;j++)
    48            fa[i][j]=fa[i-1][fa[i-1][j]];
    49 }
    50 int lca(int x,int y)
    51 {
    52     if(depth[x]>depth[y]) swap(x,y);
    53     for(int i=0;i<20;i++)
    54         if((depth[y]-depth[x])>>i&1) y=fa[i][y];
    55     if(x==y) return x;
    56     for(int i=19;i>=0;i--)
    57     {
    58         if(fa[i][x]!=fa[i][y])
    59         {
    60             x=fa[i][x];
    61             y=fa[i][y];
    62         }
    63     }
    64     return fa[0][x];
    65 }
    66 int getdis(int x,int y)
    67 {
    68     return dis[x]+dis[y]-2*dis[lca(x,y)];
    69 }
    70 int main()
    71 {
    72     int n,temp=0;
    73     while(~scanf("%d",&n))
    74     {
    75         if(temp) puts("");
    76         temp=1;
    77         cnt=0;
    78         memset(head,-1,sizeof head);
    79         for(int i=1; i<n; i++)
    80         {
    81             scanf("%d%d%d",&a,&b,&c);
    82             a++,b++;
    83             add(a,b,c);
    84             add(b,a,c);
    85         }
    86         work(n);
    87         int k;
    88         scanf("%d",&k);
    89         while(k--)
    90         {
    91             scanf("%d%d%d",&a,&b,&c);
    92             a++,b++;c++;
    93             int ans=getdis(b,a)+getdis(c,b)+getdis(a,c);
    94             printf("%d
    ",ans/2);
    95         }
    96     }
    97     return 0;
    98 }
    View Code
  • 相关阅读:
    让 vscode 作为 SpringBoot,Java,Maven,甚至于 JavaScript,C,C++,Python,Php等多语言的开发工具吧!
    MySQL 连接错误集锦
    前端开发工具库:包含事件委托,动画处理,以及大部分常用的前端工具
    初探 Node.js 框架:eggjs (环境搭配篇)
    如何快速搭建一个 Node.JS 项目并进入开发?
    关于 JavaSrcipt 前端开发的建议:模块化开发
    Spring-Session 会话共享 -> 基于 Redis 集群,内附各大错误合集,包括配置,类寻找不到、连接错误等
    Java Email 邮件发送
    CSS 显示或隐藏子元素
    Ubuntu美化及配置,常见问题解决方案(仿 Mac 风格)
  • 原文地址:https://www.cnblogs.com/csushl/p/9520461.html
Copyright © 2011-2022 走看看