博客
关于我
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/

你可能感兴趣的文章
netty——bytebuf的创建、内存分配与池化、组成、扩容规则、写入读取、内存回收、零拷贝
查看>>
netty——Channl的常用方法、ChannelFuture、CloseFuture
查看>>
netty——EventLoop概念、处理普通任务定时任务、处理io事件、EventLoopGroup
查看>>
netty——Future和Promise的使用 线程间的通信
查看>>
netty——Handler和pipeline
查看>>
Vue输出HTML
查看>>
netty——黏包半包的解决方案、滑动窗口的概念
查看>>
Netty中Http客户端、服务端的编解码器
查看>>
Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息
查看>>
Netty中实现多客户端连接与通信-以实现聊天室群聊功能为例(附代码下载)
查看>>
Netty中的组件是怎么交互的?
查看>>
Netty中集成Protobuf实现Java对象数据传递
查看>>
netty之 定长数据流处理数据粘包问题
查看>>
Netty事件注册机制深入解析
查看>>
netty代理
查看>>
Netty入门使用
查看>>
netty入门,入门代码执行流程,netty主要组件的理解
查看>>
Netty原理分析及实战(一)-同步阻塞模型(BIO)
查看>>
Netty原理分析及实战(三)-高可用服务端搭建
查看>>
Netty原理分析及实战(二)-同步非阻塞模型(NIO)
查看>>