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

题意: 给你一个无向图,然后M个询问,每次询问给你三个点,问你链接这三个点的最短距离

题解:ST算法,分别求出aa=LCA(a,b),bb=LCA(a,c),cc=LCA(c,b);最短距离就是dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc]);

//
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 55000
typedef long long ll;
const int inf = (int)1E9+;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
} //*******************************
int first[maxn],ver[maxn*];
int R[maxn*],head[maxn];
int dist[maxn],t,vis[maxn],tot;
struct ss
{
int from,to,value,next;
} e[maxn*];
int dp[maxn*][];
void init()
{
t=;
memset(head,,sizeof(head));
}
void add(int u,int v,int w)
{
ss kk= {u,v,w,head[u]};
e[t]=kk;
head[u]=t++;
}
void dfs(int u,int dep)
{
vis[u]=;
ver[++tot]=u;
first[u]=tot;
R[tot]=dep;
for(int i=head[u]; i; i=e[i].next)
{
if(!vis[e[i].to])
{
int vv=e[i].to;
dist[vv]=dist[u]+e[i].value;
dfs(vv,dep+);
ver[++tot]=u;
R[tot]=dep;
}
}
}
void find_depth()
{
tot=;
memset(vis,,sizeof(vis));
memset(first,,sizeof(first));///首次出现位置
memset(ver,,sizeof(ver));///节点编号
dfs(,);
}
void ST(int n)
{
for(int i=; i<=n; i++)dp[i][]=i;
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
int a=dp[i][j-];
int b=dp[i+(<<(j-))][j-];
dp[i][j]=R[a]>R[b]?b:a;
}
}
}
int RMQ(int x,int y)
{
int k=;
while((<<(k+))<=y-x+)
{
k++;
}
int a=dp[x][k];
int b=dp[y-(<<k)+][k];
return R[a]>R[b]?b:a;
}
int LCA(int x,int y)
{
x=first[x];
y=first[y];
if(x>y)swap(x,y);
return ver[RMQ(x,y)];
}
int main()
{
int n;
int a,b,c;
int T=;
while(scanf("%d",&n)!=EOF)
{
if(T)cout<<endl;
init();
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a+,b+,c);
add(b+,a+,c);
}
dist[]=;
find_depth();
ST(*n-);
int q=read();
for(int i=; i<=q; i++)
{
scanf("%d%d%d",&a,&b,&c);
a++;
b++;
c++;
int aa=LCA(a,b);
int bb=LCA(a,c);
int cc=LCA(b,c);
cout<<dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc])<<endl;
}
T++;
}
return ;
}

代码

ZOJ Design the city LCA转RMQ的更多相关文章

  1. ZOJ 3195 Design the city LCA转RMQ

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

  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倍增

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

  4. zoj 3195 Design the city LCA Tarjan

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

  5. [zoj3195]Design the city(LCA)

    解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...

  6. zoj——3195 Design the city

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

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

  8. xtu summer individual 1 C - Design the city

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

  9. LCA和RMQ

    下面写提供几个学习LCA和RMQ的博客,都很通熟易懂 http://dongxicheng.org/structure/lca-rmq/ 这个应该是讲得最好的,且博主还有很多其他文章,可以读读,感觉认 ...

随机推荐

  1. joda-time时间操作组件

    今天看到了学习到了一个不错的操作时间的jar包,很方便的,以后操作时间运算就可以直接使用jar包中的方法了,再也不用自己写操作时间的方法了.懒的不行不行的 <!-- 时间操作组件 --> ...

  2. 201621123082《Java程序设计》第1周学习总结

    1. 本周学习总结: 关键词: 了解Java语言的发展历史.了解Java语言的特点.JDK.JRE.JVM.eclipse等. 联系: JDK是提供给Java开发人员使用的一组工具,JDK包含JVM及 ...

  3. 启用Windows10的Linux子系统并安装图形界面

    前言 目前市面上的PC电脑主要运行着四大类系统,它们分别是微软的Windows.苹果的MacOS.Linux的发行版以及Unix类系统.其中Linux和Unix都是开源的,因此市面出现的众多基于Lin ...

  4. assert.doesNotThrow()

    assert.doesNotThrow(block[, error][, message]) 断言 block 函数不会抛出错误.查阅 assert.throws() 了解更多详情. 当调用 asse ...

  5. vue-router 根据路由动态添加目录 控制目录权限

    <template> <el-row class="el-menu" > <el-menu router :default-active='$rout ...

  6. Shader Wave

    Shader Wave 一.原理 1. 采用 UV 坐标为原始数据,生成每一条波浪线. 2. 使用 Unity 的 Time.y 作为时间增量,动态变换波形. 二.操作步骤 1. 首先使用纹理坐标生成 ...

  7. IOC&DI

    [概述] 1.IOC(Inversion of Control): 即“反转控制”,不是什么技术,而是一种设计思想.其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源.作为 ...

  8. eclipse中自动生成注释

    eclipse中自动生成注释 包前缀设置的地方 注释模板设置的地方 Eclipse自动生成方法注释 快捷键 自动生成方法的注释格式,例如 /*** @param str* @return* @thro ...

  9. JDBC的流数据

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html: PreparedStatement对象必须具备使用输入和输出流 ...

  10. Canon iP2780/iP2788 清零软件

    http://www.drvsky.com/driver/iP2780_Tools.htm http://www.dyjqd.com/soft/6085.html#download http://v. ...