4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 2107  Solved: 820
[Submit][Status][Discuss]

Description

给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

Input

第一行包含一个正整数n(2<=n<=200000),表示点数。
接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。
 
 

Output

一个整数,即最小费用。

Sample Input

5
2 2
1 1
4 5
7 1
6 7

Sample Output

2

HINT

 

Source

鸣谢Claris上传


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】的更多相关文章

  1. bzoj 4152[AMPPZ2014]The Captain

    bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...

  2. 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain

    循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...

  3. BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )

    先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...

  4. BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心

    Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...

  5. 4152: [AMPPZ2014]The Captain

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1561  Solved: 620[Submi ...

  6. 【bzoj4152】[AMPPZ2014]The Captain 堆优化Dijkstra

    题目描述 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入 第一行包含一个正整数n(2<=n< ...

  7. bzoj4152[AMPPZ2014]The Captain 最短路

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1517  Solved: 603[Submi ...

  8. [BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  9. spfa的SLF优化

    spfa的SLF优化就是small label first 优化,当加入一个新点v的时候如果此时的dis[v]比队首dis[q.front()]还要小的话,就把v点加入到队首,否则把他加入到队尾,因为 ...

随机推荐

  1. vi 编辑器使用技巧

    1.由命令"vi --version"所显示的内容知vi的全局配置文件 2.显示行号   ,非编辑模式输入 : set nu 3.显示颜色 1)在文件中找到 "synta ...

  2. fc26 url

    aarch64 http://linux.yz.yamagata-u.ac.jp/pub/linux/fedora-projects/fedora-secondary/releases/26/Ever ...

  3. TcxScheduler的使用

    TcxScheduler有两种工作模式: 一.非绑定模式 非绑定模式下,数据被存储在文件系统中.要让scheduler工作在非绑定模式下,应使TcxScheduler.Storage属性绑定到TcxS ...

  4. lombok java代码助手

    是不一个不错的代码生成工具,可以实现将代码更精简,且不失代码效率的一种不错的方法 https://www.cnblogs.com/qnight/p/8997493.html 通过java bean v ...

  5. 安装virtualenv(Scrapy)

    Windows 10家庭中文版,Python 3.6.4, virtualenv用来提供一个应用程序独立的 运行环境,这个独立是相对于系统的Python运行环境而言,开发者可以在virtualenv建 ...

  6. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

  7. 【POJ】4007.Flood-it!

    原题戳这里 题解 搜索是个好东西,不是人人都会搜 迭代加深,然后用一个函数估值,值是除了和左上连通的部分还有几个颜色不同的块,如果走的步数加上估值大于当前枚举的深度就跳出 代码 #include &l ...

  8. CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  9. 5.5版本以上”No input file specified“问题解决

    .htaccess文件中的 RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 在默认情况下会导致No input file specified. 修改成 Rewri ...

  10. C#导出HTML到PDF组件Pechkin

    http://www.knowsky.com/898441.html C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类 ...