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. 网络编程--UDP通讯

    UTP传输 public class Send1 { public static void main(String[] args) throws Exception { Scanner sc=new ...

  2. 爬虫实践--CBA历年比赛数据

    闲来无聊,刚好有个朋友来问爬虫的事情,说起来了CBA这两年的比赛数据,做个分析,再来个大数据啥的.来了兴趣,果然搞起来,下面分享一下爬虫的思路. 1.选取数据源 这里我并不懂CBA,数据源选的是国内某 ...

  3. Git: Setup a remote Git repository

    o setup a folder on a server which service for remote Git repository, apply the following steps: Cre ...

  4. Flutter之环境配置与项目搭建

    Flutter之环境配置与项目搭建 一,介绍 1.1,Dart Dart 是一种 易于学习. 易于扩展.并且可以部署到 任何地方 的 应用 编程 语言.并且同时借鉴了Java和JavaScript.D ...

  5. windows:查看电脑开放的端口

    netstat -ano netstat -ano | findstr '445' 查看445端口是否被使用 根据端口找到占用程序的PID,再用tasklist|findstr "2720& ...

  6. 【dp】B-number

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题解:先预处理([0,0][1,1],[2,2]....[0,9],[10, 19],[20,2 ...

  7. 逆向libbaiduprotect(三)- 移植python操作dalvik虚拟机c++函数,配合gdb控制程序运行流程

    python编译移植到测试机,并且移植ctypes模块.利用ctypes代替c程序,利用dalvik内部c++函数,在运行过程中手动命令操控dalvik虚拟机,并结合gdb进行调试.绕过zygote和 ...

  8. Spring Boot2 系列教程(二十七)Nginx 极简扫盲入门

    上篇文章和大家聊了 Spring Session 实现 Session 共享的问题,有的小伙伴看了后表示对 Nginx 还是很懵,因此有了这篇文章,算是一个 Nginx 扫盲入门吧! 基本介绍 Ngi ...

  9. 【2018寒假集训Day 1】【位运算】生成字符串

    生成字符串(strs) [问题描述] 假设字符串只由字符“0”,“1”,“”组成,其中字符“”表示该字符可由 字符“0”或“1”替代. 现有一些字符串,根据这些字符串生成所有可生成的字符串.如: {1 ...

  10. CSS中如何使用背景样式属性,看这篇文章就够用了

    css背景样式属性介绍 背景样式就是自定义HTML标签的背景颜色或背景图像. 背景属性说明表 属性名 属性值 描述 background-color #f00.red.rgb(255,0,0) 设置背 ...