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求任意两点间的最短距离,然后将三个结果求和然后除以二即可;画图很容易理解;
参考代码为:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int N=1e5+,maxn=+,inf=0x3f3f3f3f;
int a,b,c;
struct edge{
int to,Next,w;
}e[N];
int head[N],dis[N];
int cnt,depth[N],fa[][N];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].Next=head[u];
head[u]=cnt++;
}
void dfs(int u,int f)
{
fa[][u]=f;
for(int i=head[u];~i;i=e[i].Next)
{
int To=e[i].to;
if(To!=f)
{
depth[To]=depth[u]+;
dis[To]=dis[u]+e[i].w;
dfs(To,u);
}
}
}
void work(int n)
{
depth[]=;dis[]=;
dfs(,-);
for(int i=;i<;i++)
for(int j=;j<=n;j++)
fa[i][j]=fa[i-][fa[i-][j]];
}
int lca(int x,int y)
{
if(depth[x]>depth[y]) swap(x,y);
for(int i=;i<;i++)
if((depth[y]-depth[x])>>i&) y=fa[i][y];
if(x==y) return x;
for(int i=;i>=;i--)
{
if(fa[i][x]!=fa[i][y])
{
x=fa[i][x];
y=fa[i][y];
}
}
return fa[][x];
}
int getdis(int x,int y)
{
return dis[x]+dis[y]-*dis[lca(x,y)];
}
int main()
{
int n,temp=;
while(~scanf("%d",&n))
{
if(temp) puts("");
temp=;
cnt=;
memset(head,-,sizeof head);
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
a++,b++;
add(a,b,c);
add(b,a,c);
}
work(n);
int k;
scanf("%d",&k);
while(k--)
{
scanf("%d%d%d",&a,&b,&c);
a++,b++;c++;
int ans=getdis(b,a)+getdis(c,b)+getdis(a,c);
printf("%d\n",ans/);
}
}
return ;
}

ZOJ 3195 Design the city (LCA 模板题)的更多相关文章

  1. zoj 3195 Design the city lca倍增

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

  2. zoj 3195 Design the city LCA Tarjan

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

  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

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

  5. ZOJ 3195 Design the city 题解

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

  6. ZOJ - 3195 Design the city

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

  7. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. ZOJ Design the city LCA转RMQ

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

  9. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. js调用浏览器“打印”与“打印预览”

    用到html <object>标签,具体做法如下: 1.在html文档任意位置添加<object>标签: <div style="border: 1px sol ...

  2. 比较一下inner join(可直接简写为join)和where直接关联

    SELECT * FROM A ,B WHERE A.ID = B.ID 是比较常用的2个表关联.之前一直用这个,后来换了家公司发现这家公司的报表大多数都是用inner join,稍微研究了一下.查阅 ...

  3. C++中对C的扩展学习新增语法——动态内存管理

    1.C语言动态内存管理的缺点: 1.malloc对象的大小需要自己计算. 2.需要手动转换指针类型. 3.C++的对象不适合使用malloc和free. 2.C++中new/delete基本使用: 3 ...

  4. Python3.7.1(四) Print如何在输出中插入变量

    # 如果想在打印的字符串中的任意地方加入任意的变量,可以使用python的格式化输出.## 用例代码如下:s = 'Hello'x = len(s)print("The length of ...

  5. 移动端自动化测试Appium环境搭建(part1-2-3)

    Appium移动端自动化测试相信大家都不陌生,appium的铁哥们是selenium,不管是selenium还是appium,都是调用webdriver来做自动化测试.今天关于appium的介绍我们不 ...

  6. this绑定方式总结

    最近在回顾js的一些基础知识,把<你不知道的js>系列又看了一遍,this始终是重中之重,还是决定把this相关知识做一个系统的总结,也方便自己日后回顾. this的四条绑定规则 1.默认 ...

  7. 在Raspberry Pi上创建容器

    树莓派Raspbian默认是支持LXC容器的,下面我们介绍一下在树莓派上创建并运行容器的过程. 1. 安装LXC相关的package $ sudo apt-get install -y git lxc ...

  8. python3之递归实例

    一.利用递归求: 1+2+3+4+5...+n的前n项和 def recursion_sum_1(n): #当n = 1:和为1 #否则,n的和等同于 n + (n -1) if n == 1: re ...

  9. ubuntu 交叉编译 busybox 1.31.1

    目的:静态编译 Busybox_arm64 1.13.1 环境:Ubuntu 18.04.3 #----------------环境配置 # aarch64-linux-gnu-g++ sudo ap ...

  10. java struts2 debug

    出了一堆bug 改的顺序和哪个起了作用不太记得了 下面列出遇到问题的顺序:1java.lang.NoSuchMethodException:没这个方法 代码是改正后的,出错的时候保证class ,ac ...