【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点加入到队首,否则把他加入到队尾,因为 ...
随机推荐
- jQuery动态给下拉列表添加一个选项(创建DOM对象)
使用的函数:
- .net HttpCrawler
using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Diagnostics; usi ...
- 『实践』Java Web开发之分页(ajax)
1.需要用到的jar包.js文件 JSONArray().fromObject()需要的jar包: (1)commons-beanutils-1.8.3.jar (2)commons-collecti ...
- Nginx实现代理和用户验证
1.下载Nginx 首先去官网http://nginx.org/en/download.html下载需要的版本即可,无需安装,只需要打开nginx.exe文件,nginx.exe的服务就开启了.打开h ...
- git本地分支和远程分支改名
#1 将本地分支进行改名 git branch -m old_branch new_branch #2 将远程分支的老分支删除 git push origin :old_branch #3 将改名后的 ...
- tf.summary.merge_all()
1.自动管理模式 summary_writer = tf.summary.FileWriter('E:/data/tensorflow-master/1.Cnn_Captcha/result/', f ...
- sqlserver中的全局变量总结
@@CONNECTIONS返回自上次启动 Microsoft? SQL Server? 以来连接或试图连接的次数.@@CPU_BUSY返回自上次启动 Microsoft? SQL Server? 以来 ...
- How to tell your iPhone application that location services are required | The Agile Warrior
div{padding-bottom:10px}.b_vPanel>div:last-child{padding:0}.banner a{color:#1020d0} --> Below ...
- Error_code: 1594(mysql 5617)主从同步报错
报错信息 2017-09-05 09:37:22 7425 [ERROR] Slave SQL: Relay log read failure: Could not parse relay log e ...
- Spark(二)CentOS7.5搭建Spark2.3.1分布式集群
一 下载安装包 1 官方下载 官方下载地址:http://spark.apache.org/downloads.html 2 安装前提 Java8 安装成功 zookeeper 安 ...