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. 学习笔记 第七章 使用CSS美化超链接

    第7章  使用CSS美化超链接 学习重点 认识超链接 熟悉伪类 定义超链接样式 能够灵活设计符合页面风格的链接样式 7.1  定义超链接 在HTML5中建立超链接需要两个要素:设置为超链接的网页元素和 ...

  2. Android开发-下载网络图片并显示到本地

    Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...

  3. 使用VC++编写QQ群发器,MFC做UI

    由于公司业务需要,QQ群发器经常被用来发送QQ广告,购买的QQ群发器不好用不说,而且是按机器收费的,有些功能还不能修改. 所以公司派我来开发一款自己的QQ群发器,我给群发器取名叫做飞速群发器,用来给软 ...

  4. linux 小键盘 数字键盘 wiki

    https://wiki.archlinux.org/index.php/Activating_Numlock_on_Bootup_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96 ...

  5. S​Q​L​_​S​e​r​v​e​r​_​2​0​0​8​定​期​自​动​备​份​详​细​图​解

    S​Q​L​_​S​e​r​v​e​r​_​2​0​0​8​定​期​自​动​备​份​详​细​图​解 设置自动数据库的定期备份计划. http://wenku.baidu.com/link?url=Tu ...

  6. JVM内存管理及垃圾回收机制

    一.JVM内存组成结构 JVM栈由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示:  二.JVM内存回收 Sun的JVMGenerationalCollecting(垃圾回收)原理是这样的:把对 ...

  7. python 列表生成式、lower()和upper()的使用

    参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868196389 ...

  8. 关于js事件冒泡和事件捕获

    事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事件触发顺序是否为事件捕获.true,事件捕获: ...

  9. CSU2188: Substring

    Description FST 是一名可怜的 ACMer,他很强,但是经常 fst,所以 rating 一直低迷. 但是重点在于,他真的很强!他发明了一种奇特的加密方式,这种加密方式只有 ACMer ...

  10. js 技巧 (二)

    //最小化,最大化,关闭 <object id=min classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  & ...