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

------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
  
#define rep(i, n) for(int i = 0; i < n; i++)
#define clr(x, c) memset(x, c, sizeof(x))
  
using namespace std;
 
const int inf = 1000000009, maxn = 200009;
 
struct edge {
int to, w;
edge*next;
} E[maxn << 2], *pt = E, *head[maxn];
 
struct node {
int x, d;
bool operator < (const node&o) const {
return d > o.d;
}
};
 
struct P {
int x, y;
inline void Read() {
scanf("%d%d", &x, &y);
}
} A[maxn];
 
inline void add(int u, int v, int w) {
pt->to = v, pt->w = w;
pt->next = head[u];
head[u] = pt++;
}
#define add_edge(u, v, w) add(u, v, w), add(v, u, w)
 
bool cmpX(const int i, const int j) {
return A[i].x < A[j].x;
}
 
bool cmpY(const int i, const int j) {
return A[i].y < A[j].y;
}
 
int d[maxn], n, X[maxn], Y[maxn];
priority_queue<node> Q;
 
void dijkstra() {
rep(i, n) d[i] = inf;
d[0] = 0, Q.push( (node) {0, 0} );
while(!Q.empty()) {
node t = Q.top(); Q.pop();
if(d[t.x] != t.d) continue;
for(edge*e = head[t.x]; e; e = e->next) if(d[e->to] > d[t.x] + e->w) {
d[e->to] = d[t.x] + e->w;
Q.push( (node) {e->to, d[e->to]} );
}
}
}
 
int main() {
freopen("test.in", "r", stdin);
cin >> n;
rep(i, n) {
   A[i].Read();
   X[i] = Y[i] = i;
}
sort(X, X + n, cmpX);
rep(i, n - 1) {
P*a = A + X[i], *b = A + X[i + 1];
if(b->x - a->x <= abs(a->y - b->y)) add_edge(X[i], X[i + 1], b->x - a->x);
}
sort(Y, Y + n, cmpY);
rep(i, n - 1) {
P*a = A + Y[i], *b = A + Y[i + 1];
if(b->y - a->y <= abs(a->x - b->x)) add_edge(Y[i], Y[i + 1], b->y - a->y);
}
dijkstra();
printf("%d\n", d[n - 1]);
return 0;
}

------------------------------------------------------------------------

4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 272  Solved: 104
[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

BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )的更多相关文章

  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 Dijkstra+贪心

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

  4. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

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

  5. bzoj4152[AMPPZ2014]The Captain 最短路

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

  6. 4152: [AMPPZ2014]The Captain

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

  7. 【BZOJ4152】[AMPPZ2014]The Captain 最短路

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

  8. BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*

    BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...

  9. 『The Captain 最短路建图优化』

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

随机推荐

  1. Qt5 FOR WINCE7, Visual Studio 2008环境的搭建

    Qt5 FOR WINCE7, Visual Studio 2008环境的搭建 Qt5发布时,试过配置Qt5 for wince的环境,原因是暂时不支持WINCE.前几天意外发现官方博客说明已经开始支 ...

  2. 在CheckBox中,仅仅允许选择一项

    作用相当于RadioButonList <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=" ...

  3. Cramfs、JFFS2、YAFFS2的全面对比

    Cramfs.JFFS2.YAFFS2的全面对比http://blog.csdn.net/daofengdeba/article/details/7721340 由于嵌入式系统自身存在一些特殊要求,使 ...

  4. PreTranslateMessage和TranslateMessage区别(转)

    PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当需要在MFC之前处理某些消息时,常常要在这里添加代码. ...

  5. 复制virtualenv环境到其他服务器环境配置的方法

    要在n多服务器端部署python的应用,虽然python本身是跨平台的,当时好多第三方的扩展却不一定都能做到各个版本兼容,即便是都是linux,在redhat系列和ubuntu系列之间来回导也是个很让 ...

  6. tf–idf算法解释及其python代码实现(上)

    tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...

  7. Android dp和sp的用法汇总

    1 > dp 是跟像素密度无关的单位,也就是说在相同尺寸.但不同的分辨率的手机上,用dp标识的东西,显示的大小是一样的. sp是用于标识字体的,它不仅跟屏幕尺寸有关,还跟设置的系统字体大小有关. ...

  8. Ruby学习: 类变量和类方法

    一.类变量 在ruby中,可以为类定义类变量,类变量的值为类的所有实例(对象)所共享. 有点类似其它语言(如java)中的静态变量,但与java中的静态变量的区别是, 类变量是私有的,无法在类的外部访 ...

  9. docker 数据盘映射方案

    docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录, :前面的/data为本地目录,:后面的/data1 为容器里的目录: dock ...

  10. hdu 4730 We Love MOE Girls

    http://acm.hdu.edu.cn/showproblem.php?pid=4730 直接用string类处理字符串. AC代码: #include<iostream> #incl ...