博客
关于我
zoj 3195 Design the city LCA Tarjan
阅读量:443 次
发布时间:2019-03-06

本文共 1358 字,大约阅读时间需要 4 分钟。

题目大意:

求三点之间的最短距离

思路:

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

对于三个点我们两两之间求最短距离 得到 d1 d2 d3
那么最短距离就是 d = ( d1 + d2 + d3 ) / 2

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

代码:

#include 
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

转载地址:http://zmjyz.baihongyu.com/

你可能感兴趣的文章
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
Vue3.0中的响应式原理(第九课)
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
NIS认证管理域中的用户
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 时事和见解【2023】
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>