先按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. 基于RYU控制器(controller)上的simple-switch 的APP做的測试-SDN/OpenFlow

    近期一直在学习RYU控制器,在使用的过程中,发现有下面几方面的长处:RYU控制器全然使用Python语言编写,在理解起来和上手速度上是挺快的:RYU控制器的总体架构清晰明了,在日后有时间我会整理一个关 ...

  2. Ext JS学习第五天 Ext_window组件(一)

    此文来记录学习笔记 •第一个组件:Ext.window.Window.对于组件,也就是Ext最吸引开发者的地方,那么我们要真正的使用Ext的组件,首先必须学会阅读API文档. –xtype:组件的别名 ...

  3. linq to sql用partial扩展属性,创建一个部分类(用于多表连接)

    1.在窗体中创建dataGridView显示表: using System; using System.Collections.Generic; using System.ComponentModel ...

  4. android四大组件详解--摘

    Android四大基本组件介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器 ...

  5. md5 加密 swfit版

    在swift工程中随便建一个objective-c类,会提示你生成一个Bridging-Header,点YES,然后删除刚才建立的objective-c类,只留下[工程名]-Bridging-Head ...

  6. C# Programming Study #2

    readonly (C# Reference) readonly  关键字是可以在字段上使用的修饰符.  当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者 ...

  7. 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)

    Dijkstra算法 ———————————最后更新时间:2011.9.25———————————Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径. ...

  8. GridView用法的修改和删除

    (前台) <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...

  9. pycurl,Python cURL library

    pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...

  10. 将HDC保存为BMP文件

    HDC在MSDN中的全称为:The handle of device context.通常,我们都是用来做相应的显示操作.        熟悉WIN32的朋友对于其应该不会陌生,经常采用GetDC,G ...