【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 2107 Solved: 820
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 2
1 1
4 5
7 1
6 7
Sample Output
HINT
Source
Solution
这道题本身很简单,分别按$x$、$y$排序相邻间建边跑最短路就可以了,可是这道题卡$Spfa$,可以用$Dijistra$,这里学习了一种玄学(?)优化
SLF优化$Spfa$,如果当前加入的点$dis$比队首小就放到队首,其它照常放在队尾,最优可以卡到$O(nlog_n)$
Code
#include<bits/stdc++.h>
#define LL long long
using namespace std; const int N = ; struct ED {
int v, nex, w;
} Edge[]; int h[], stot;
void add(int u, int v, int w) {
Edge[++stot].v = v, Edge[stot].nex = h[u], Edge[stot].w = w;
h[u] = stot;
} int n;
struct Node {
int x, y, id;
} a[];
bool cmp1(Node a, Node b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x;}
bool cmp2(Node a, Node b) { if(a.y == b.y) return a.x < b.x; return a.y < b.y;} LL dis[];
int vis[], q[];
void spfa() {
memset(dis, 0x3f3f3f3f, sizeof(dis));
vis[] = ; dis[] = ;
int s = , t = , tot = ;
q[++t] = ;
while(tot) {
int x = q[s]; tot --; s = (s + ) % N;
vis[x] = ;
for(int i = h[x]; i; i = Edge[i].nex) {
int v = Edge[i].v;
if(dis[v] > dis[x] + Edge[i].w) {
dis[v] = dis[x] + Edge[i].w;
if(!vis[v]) {
vis[v] = ; tot ++;
if(dis[v] <= dis[q[s]]) {
s = (s - + N) % N;
q[s] = v;
} else {
t = (t + ) % N;
q[t] = v;
}
}
}
}
}
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i ++) scanf("%d%d", &a[i].x, &a[i].y), a[i].id = i;
sort(a + , a + + n, cmp1);
for(int i = ; i < n; i ++) {
int a1 = a[i].x, a2 = a[i + ].x, b1 = a[i].y, b2 = a[i + ].y;
add(a[i].id, a[i + ].id, a2 - a1); add(a[i + ].id, a[i].id, a2 - a1);
}
sort(a + , a + + n, cmp2);
for(int i = ; i < n; i ++) {
int a1 = a[i].x, a2 = a[i + ].x, b1 = a[i].y, b2 = a[i + ].y;
add(a[i].id, a[i + ].id, b2 - b1); add(a[i + ].id, a[i].id, b2 - b1);
}
spfa();
printf("%lld", dis[n]);
return ;
}
【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】的更多相关文章
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心
Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...
- 4152: [AMPPZ2014]The Captain
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1561 Solved: 620[Submi ...
- 【bzoj4152】[AMPPZ2014]The Captain 堆优化Dijkstra
题目描述 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入 第一行包含一个正整数n(2<=n< ...
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- [BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- spfa的SLF优化
spfa的SLF优化就是small label first 优化,当加入一个新点v的时候如果此时的dis[v]比队首dis[q.front()]还要小的话,就把v点加入到队首,否则把他加入到队尾,因为 ...
随机推荐
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...
- java基础49 IO流技术(对象输入流/对象输出流)
1.对象输入输出流 对象注意作用是用于写对象信息与读取对象信息 1.对象输出流:ObjectOutputStream 2.对象输入流:ObjectInputStream 2.对象输入输出流的步骤 ...
- 洛谷P1266速度限制
传送门啦 看起来是一个最短路问题,但是引入了速度限制,就要写一下二维最短路了. $ dis[i][j] $ :表示到i这个点,速度为j的最短时间. #include <iostream> ...
- Java事务管理之Hibernate
环境与版本 Hibernate 版本:Hibernate 4.2.2 (下载后的文件名为hibernate-release-4.2.2.Final.zip,解压目录hibernate-release- ...
- NFS服务简介
NFS服务简介 NFS是Network File System的缩写,即网络文件系统.NFS是由Sun开发并发展起来的一项用于在不同机器,不同操作系统之间通过网络互相分享各自的文件.NFS serve ...
- WORDPRESS登录后台半天都无法访问或者是访问慢的解决方法
wordpress登录后台如果打开速度慢,一般分为两部分,第一部分是php虚拟主机的原因,其中主机的原因,又分为很多种情况.第二部分就是WordPress程序本身的问题.这里无忧主机小编主要是讲第二部 ...
- 使用Oracle数据库,对某个表频繁更新
使用Oracle数据库,对某个表频繁更新,查询时要联合这张表,查询速度非常慢,有什么解决办法? 一般的pc机oracle更新的效率均可达到500+/s, 可能的问题,你更新这个不会是每次都新建jdbc ...
- 参数化SQL语句
避免SQL注入的方法有两种:一是所有的SQL语句都存放在存储过程中,这样不但可以避免SQL注入,还能提高一些性能,并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理,不过这种做法有时候针对相 ...
- 微信接口问题(The underlying connection was closed: An unexpected error occurred on a send)
突然在调用微信接口是报:The underlying connection was closed: An unexpected error occurred on a send错误,跟踪了半天,是因为 ...
- 多线程-TaskExecutor-使用Demo
BasicExecute.java package com.jef.executeTest; public abstract class BasicExecute extends Thread { @ ...