题目链接 : ZOJ Problem Set - 3195

题目大意:

求三点之间的最短距离

思路:

有了两点之间的最短距离求法,不难得出:

对于三个点我们两两之间求最短距离 得到 d1 d2 d3

那么最短距离就是 d = ( d1 + d2 + d3 ) / 2

  • 要注意每个数组的范围大小,因为这个问题手抖敲错,TLE+RE一整页/(ㄒoㄒ)/~~
  • 用前向星来保存边和询问,空间卡的也很严
  • 如下图所示:所求路线为紫色,等于蓝色+黄色+绿色之和的一半

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
const int maxm = 70005;
struct node1 {
int next,to,w;
} e[maxn*2];
struct node2 {
int next,to,id;
} q[maxm*6];
int n,m,head1[maxn],head2[maxn],cnt1,cnt2,vis[maxn],f[maxn],res[maxm*6],dist[maxn];
inline void add1(int u, int v, int w) {
e[cnt1].to=v;
e[cnt1].w=w;
e[cnt1].next=head1[u];
head1[u]=cnt1++;
}
inline void add2(int u, int v, int id) {
q[cnt2].to=v;
q[cnt2].id=id;
q[cnt2].next=head2[u];
head2[u]=cnt2++;
}
inline void init() {
cnt1=cnt2=0;
memset(head1,-1,sizeof(head1));
memset(head2,-1,sizeof(head2));
memset(vis,0,sizeof(vis));
}
inline int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
inline void tarjan(int s) {
vis[s]=1;
f[s]=s;
int t;
for(int i=head1[s]; i!=-1; i=e[i].next) {
if(!vis[t=e[i].to]) {
dist[t]=dist[s]+e[i].w;
tarjan(t);
f[t]=s;
}
}
for(int i=head2[s]; i!=-1; i=q[i].next)
if(vis[t=q[i].to])
res[q[i].id]=dist[s]+dist[t]-2*dist[Find(t)];
}
int main() {
int cnt=0,u,v,w,x,y,z;
while(~scanf("%d",&n)) {
init();
for(int i=1; i<n; ++i) {
scanf("%d %d %d",&u,&v,&w);
add1(u,v,w);
add1(v,u,w);
}
scanf("%d",&m);
m*=3;
for(int i=1; i<=m; ++i) {
scanf("%d %d %d",&x,&y,&z);
add2(x,y,i);
add2(y,x,i);
++i;
add2(x,z,i);
add2(z,x,i);
++i;
add2(y,z,i);
add2(z,y,i);
}
dist[0]=0;
tarjan(0);
if(!cnt) cnt++;
else printf("\n");
for(int i=1; i<=m; ++i) {
printf("%d\n",(res[i]+res[i+1]+res[i+2])/2);
i+=2;
}
}
return 0;
}

zoj 3195 Design the city LCA Tarjan的更多相关文章

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

  2. ZOJ 3195 Design the city LCA转RMQ

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

  3. zoj 3195 Design the city lca倍增

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

  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. 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(LCA)

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

  9. 最近公共祖先LCA(Tarjan算法)的思考和算法实现

    LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...

随机推荐

  1. MySQL复制之理论篇

    一.MySQL复制概述 MySQL支持两种复制方式:基于行的复制和基于语句的复制(逻辑复制).这两种方式都是通过在主库上记录 二进制日志.在备库重放日志的方式来实现异步的数据复制,其工作原理如下图: ...

  2. Java正则表达式详解+练习

    一.导读 正则表达式,又称规则表达式.(英文名Regular Expression,所以代码中常以regex.regexp.RE表示).正则表达式简单说就是用于操作文本数据的规则表达式,在Java中我 ...

  3. spark-shell启动报错:Yarn application has already ended! It might have been killed or unable to launch application master

    spark-shell不支持yarn cluster,以yarn client方式启动 spark-shell --master=yarn --deploy-mode=client 启动日志,错误信息 ...

  4. Vue源码后记-其余内置指令(3)

    其实吧,写这些后记我才真正了解到vue源码的精髓,之前的跑源码跟闹着玩一样. go! 之前将AST转换成了render函数,跳出来后,由于仍是字符串,所以调用了makeFunction将其转换成了真正 ...

  5. DNS主从服务部署

    (1)节点信息 console01 主DNS 192.168.80.3 192.168.10.3 console02 从DNS 192.168.80.4 192.168.10.4 (2)环境部署 # ...

  6. linux GCC 编译多个.c/.h文件

    基本认识: #include <xxx>:首先去系统目录中找头文件,如果没有在到当前目录下找.像标准的头文件 stdio.h.stdlib.h等用这个方法. #include " ...

  7. HTTP服务简介

    第1章 HTTP服务介绍 1.1 简述用户访网站流程 a 进行域名信息的DNS解析   dig +trace 获得www.oldboyedu.com  ip地址信息 b 进行与网站服务器建立连接,tc ...

  8. Python基础学习参考(三):内置函数

    一:内置函数 在第一篇文章中,我们简单的认识了一下print()函数和input()函数,也就是输入和输出,这些函数我们可以直接的调用,不要自己定义或者引入什么,对吧?想这样的函数就叫做内置函数.这里 ...

  9. 数组删除操作 splice

    原理通过设置 函数的  length 属性 var  a = [1, 2, 3, 4]; a.length = 3 ; 结果  :   a  =  [1,2,3]

  10. NULL字段对于UNIQUE INDEX失效