先按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. Score(规律)

    Score Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  2. java解析xml的几种方式

    java解析xml的几种方式 DOM DOM的全称是Document ObjectModel,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称D ...

  3. 如何判断一个变量是数组Array类型

    在很多时候,我们都需要对一个变量进行数组类型的判断.JavaScript中如何判断一个变量是数组Array类型呢?我最近研究了一下,并分享给大家,希望能对大家有所帮助. JavaScript中检测对象 ...

  4. backbone HTTP方法中 options参数

    wait: 可以指定是否等待服务端的返回结果再更新model.默认情况下不等待url: 可以覆盖掉backbone默认使用的url格式attrs: 可以指定保存到服务端的字段有哪些,配合options ...

  5. HTML+CSS基础总结

    1. 首先声明正确的文档类型doctype 通常有四种文档类型可供选择: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ...

  6. 获得view所在的控制器

    - (UIViewController*)getViewController{ for (UIView* next = [self superview]; next; next = next.supe ...

  7. C#用正则表达式 获取网页源代码标签的属性或值

    1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...

  8. 子进程管理模块subprocess

    subprocess模块允许你生成子进程,连接管道,并获取返回的代码. 一.使用subprocess模块 模块中定义了一个Popen类:       subprocess.Popen(args, bu ...

  9. HTML+CSS笔记 表格,超链接,图片,表单

    表格 给表格加入CSS样式,添加表格边框 语法: <style type="text/css"> table tr td,th{border:1px solid #00 ...

  10. Regex阅读笔记(二)之环视

    环视不匹配任何字符,只匹配文本中的特定位置. 正序环视:(?=) 逆序环视:(?<=) 非捕获(?:) 环视会检查子表达式能否匹配,但它只寻找能够匹配的位置,而不会真正占用这些字符. -用在字符 ...