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

题目大意:给一个无根树,有q个询问,每个询问3个点,问将这3个点连起来,距离最短是多少

思路:询问3各点之间的最短距离与两个点之间的最短距离相同,我们只需要处理出lca(x,y),lca(y,z),  lca(x,z) 然后再求这些点两两之间的最短距离,然后加起来除以2就好了。

鬼畜的输出、、、、

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 51000
using namespace std;
int n,m,x,y,z,ans,sum,tot,ans1,ans2,ans3;
int fa[N],top[N],size[N],deep[N],head[N],dis[N];
struct Edge
{
    int to,dis,from,next;
}edge[N<<];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
      if(deep[top[x]]<deep[top[y]])
        swap(x,y);
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
int dfs(int x)
{
    size[x]=;
    deep[x]=deep[fa[x]]+;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to)
        {
            dis[to]=dis[x]+edge[i].dis;
            fa[to]=x,dfs(to);
            size[x]+=size[to];
        }
    }
}
int dfs1(int x)
{
    ;
    if(!top[x]) top[x]=x;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&size[t]<size[to]) t=to;
     }
     if(t) top[t]=top[x],dfs1(t);
     for(int i=head[x];i;i=edge[i].next)
     {
         int to=edge[i].to;
         if(fa[x]!=to&&to!=t) dfs1(to);
     }
}
int begin()
{
    ans=;tot=;
    memset(fa,,sizeof(fa));
    memset(top,,sizeof(top));
    memset(dis,,sizeof(dis));
    memset(edge,,sizeof(edge));
    memset(deep,,sizeof(deep));
    memset(size,,sizeof(size));
    memset(head,,sizeof(head));
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(sum++) printf("\n");
        begin();
        ;i<n;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z),add(y,x,z);
        }
        dfs(),dfs1();
        m=read();
        ;i<=m;i++)
        {
            x=read(),y=read(),z=read();
            ans1=dis[x]+dis[y]-*dis[lca(x,y)];
            ans2=dis[x]+dis[z]-*dis[lca(x,z)];
            ans3=dis[y]+dis[z]-*dis[lca(y,z)];
            ans=(ans1+ans2+ans3)/;
            printf("%d\n",ans);
        }
    }
    ;
}

zoj——3195 Design the city的更多相关文章

  1. zoj 3195 Design the city LCA Tarjan

    题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...

  2. 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 terribl ...

  3. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

  4. ZOJ - 3195 Design the city

    题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...

  5. zoj 3195 Design the city lca倍增

    题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...

  6. ZOJ 3195 Design the city 题解

    这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据  1 < N < 50000  1 < Q ...

  7. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  8. ZOJ3195 Design the city [2017年6月计划 树上问题04]

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  9. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

随机推荐

  1. 技术杂记之:vi使用入门

    对于Linux的初次使用者来说,进入Linux非图形界面后,不知道怎么创建文本(甚至于在图形界面,也找不到创建文本的菜单).其实,每一个Linux的发行版本,都包含了一个最简单.也是最基础的文本编辑器 ...

  2. js中,浏览器中不同元素位置属性解析

    offset()   只对可见元素有效,获取匹配元素在当前视口的相对偏移,返回的对象有两个整型属性,top和left,像素计算: position() 相对父元素的偏移,position.left   ...

  3. flex布局(主要分清楚容器和条目)

    设置在容器上面的属性:flex-direction.flex-wrap.flex-flow.justify-content.align-items.align-content1.flex-direct ...

  4. PowerDesigner 操作手册

    1.错误信息:Generation aborted due to errors detected during the verification of the model 解决方案: 把检查模型的选项 ...

  5. sqlalchemy ORM进阶- 批量插入数据

    参考: https://www.jb51.net/article/49789.htm https://blog.csdn.net/littlely_ll/article/details/8270687 ...

  6. 谷歌全屏脚本 start chrome.exe --kiosk http://www.baidu.com

    start chrome.exe --kiosk http://www.baidu.com

  7. LAME的“命令行”

    VBR 编码 (强烈推荐) Alt Preset Extreme (平均256kbps) 我们有时在网上可以看到".LAME-APX." 就是这种形式,我们也可以在文件名中包含这个 ...

  8. IIS+php服务器无法上传图片解决办法

    查找网上资料,发现php.ini下面有2个地方关于上传的配置: file_uploads = On  这里设置是否允许HTTP上传,默认应该为ON的 ;upload_tmp_dir=  这里设置上传文 ...

  9. pytorch之Tensor与Variable的区别

    首先在变量的操作上:Tensor对象支持在原对象内存区域上修改数据,通过“+=”或者torch.add()方法而Variable不支持在原对象内存区域上修改数据Variable对象可求梯度,并且对Va ...

  10. React开发实时聊天招聘工具 -第四章 Redux

    复杂以后 setState 就不太方便了 所以使用Redux来管理 React只负责View. Store.State.Dispatch.Reducer reducer(state,action) { ...