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. 面试之Redis

    面:缓存中间件--Memcached和Redis的区别是什么? 答:Memcached的优点是简单易用,代码层次类似与Hash.支持简单数据类型,但不支持数据持久化存储,也不支持主从同步,也不支持分片 ...

  2. 浅谈树套树(线段树套平衡树)&学习笔记

    0XFF 前言 *如果本文有不好的地方,请在下方评论区提出,Qiuly感激不尽! 0X1F 这个东西有啥用? 树套树------线段树套平衡树,可以用于解决待修改区间\(K\)大的问题,当然也可以用 ...

  3. mysql多表合并为一张表

    有人提出要将4张表合并成一张.数据量比较大,有4千万条数据.有很多重复数据,需要对某一列进行去重. 数据量太大的话,可以看我另外一篇:http://www.cnblogs.com/magmell/p/ ...

  4. token 的生成杂谈

    背景 很多时候我们需要用 token 来作为一些标识, 比如: 一个用户登录后的认证标识. 实现方式 md5 的方式: $v = 1; // 自己定义的 需要hash 的value 值 $key = ...

  5. HDU 4747 Mex【线段树上二分+扫描线】

    [题意概述] 一个区间的Mex为这个区间没有出现过的最小自然数,现在给你一个序列,要求求出所有区间的Mex的和. [题解] 扫描线+线段树. 我们在线段树上维护从当前左端点开始的前缀Mex,显然从左到 ...

  6. 51NOD 2368 珂朵莉的旅行

    >>这是原题传送门<< 答案参考来自 http://www.cnblogs.com/sugewud/p/9822933.html 思路:思维题OR规律题?个人没写出来,脑子里只 ...

  7. 2017 GDCPC 省赛总结

    第一年参加省赛,也是我接触acm半年多的第一个正式省级赛事,这半年来我为acm付出的可能很多,但经历过这次省赛后,给我唯一的感觉就是,还不够多. 直接分析题目吧,开始拿到试题后我读的是A题,然后我的队 ...

  8. 找到多个与名为“Home”的控制器匹配的类型。

    原因分析 其实上面已经讲的很清楚了,找到了两个同名Home控制器,需要配置命名空间来区分. 解决方法 方法一:修改RouteConfig.cs 方法二:修改RouteConfig.cs 和 Admin ...

  9. STM32F407 IO引脚复用器和映射 个人笔记

    基本概念 stm32有一些内置外设,每个外设有一个复用功能AF(Alternate functions). stm32的每个io引脚东路有一个16路复用器,该复用器一端连该引脚,另外16端连AF0~A ...

  10. Java语言编写TPL语言词法分析器

    程序实现原理: 将TXT文本中的数据读出,并按照其类别的不同,将关键字.数字以及运算符识别出来. 一.词法分析实验步骤 1. 熟悉TPL语言 2. 编写TPL语言程序,至少3个,一个简单,一个复杂的( ...