zoj 3195 Design the city LCA Tarjan
题目大意:
求三点之间的最短距离
思路:
有了两点之间的最短距离求法,不难得出:
对于三个点我们两两之间求最短距离 得到 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的更多相关文章
- 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 ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- zoj 3195 Design the city lca倍增
题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ 3195 Design the city 题解
这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据 1 < N < 50000 1 < Q ...
- ZOJ - 3195 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- [zoj3195]Design the city(LCA)
解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...
- 最近公共祖先LCA(Tarjan算法)的思考和算法实现
LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...
随机推荐
- xml文件的方式实现动态代理基于SpringAOP
1.配置spring容器 导入jar包 com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.j ...
- Appium python自动化测试系列之混合app实战(十一)
12.1 什么是混合App 12.1.1 混合app定义 什么是混合app,其实这个不言而喻,我们的app正常来说应该都是native的,但是实际工作中却不是,反正种种原因我们的app会有native ...
- 紧跟腾讯大王卡:B站2233卡“基友号”即将上线
来自B站官方的消息显示,B站在近期也将推出"基友号"功能,功能的内容是通话免费,考虑到这类互联网套餐都是中国联通在运营,因此在内容上可能也会效仿腾讯大王卡实现免费通话. 目前,B站 ...
- JavaScript Function.call() 函数详解
语法 functionObject.call( [ thisObj [, arg1 [, arg2 [, args...]]]] ) call()函数用于调用当前函数functionObject,并可 ...
- sass学习--在htm文件中使用
一.导语 最近的战狼2好火爆啊,每天看战狼2的票房一路高飙,我估计比吴京还开心.看了这部戏的拍摄过程,除了敬佩就是踏实,是的,吴京是电影圈隔了这么久后能踏踏实实做电影的了,纯属个人见解,不喜请忽略.. ...
- NHibernate查询示例合集
基本查询 复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...
- KVM管理平台openebula安装
1.1opennebula控制台的安装 (如果要添加映像需要给200G以上给/var/lib/one,本文是共享/var/lib/one实现监控,用映像出创建虚拟机原理是从opennebula控制平台 ...
- 【Kafka源码】broker被选为controller之后的连锁反应
[TOC] 今天我们主要分析下broker被选为controller之后,主要干了什么.门面代码先列出来: def onControllerFailover() { if (isRunning) { ...
- 关于thinkphp控制器引用model里的方法的一点收获
有时候真的是很绕,为了这一点点收获,我几乎花了一天的时间.当我弄明白了的那一刻,我.........好吧,写到这里,我发现不能换行.好吧,就这样吧,开始说正题:要想在controler从model引用 ...
- C#中迭代器的概念和两种实现方式
1.首先我们看下IEnumerable接口定义: namespace System.Collections { // Summary: // Expose ...