Description

给定平面上的 \(n\) 个点,定义 \((x_1,y_1)\) 到 \((x_2,y_2)\) 的费用为 \(\min(|x_1-x_2|,|y_1-y_2|)\) ,求从 \(1\) 号点走到 \(n\) 号点的最小费用。

Input

第一行包含一个正整数 \(n(2\le n\le 200000)\) ,表示点数。

接下来 \(n\) 行,每行包含两个整数 \(x[i],y[i] (0\le x[i],y[i]\le 10^9)\) ,依次表示每个点的坐标。

Output

一个整数,即最小费用。

Sample

Sample Input

5
2 2
1 1
4 5
7 1
6 7

Sample Output

2

Solution

这傻逼题吧...这种题都出烂了...

写这道题的原因是听说这道题卡 \(\mathrm{spfa}\) 。

#include<bits/stdc++.h>
using namespace std;#define N 200001
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define pli pair<ll, int>inline int read() {int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
}struct point { int x, y, id; }a[N];
bool cmpx(const point& p1, const point& p2) { return p1.x < p2.x; }
bool cmpy(const point& p1, const point& p2) { return p1.y < p2.y; }
int n, head[N], tot;
ll dis[N];
bool vis[N];
priority_queue<pli, vector<pli>, greater<pli> > q;
struct edge{ int v, next; ll w; }e[N << 2];
inline void insert(int u, int v, ll w) { e[++tot].v = v, e[tot].w = w, e[tot].next = head[u], head[u] = tot; }
#define add(u, v, w) insert(u, v, w), insert(v, u, w)void dijkstra() {rep(i, 2, n) dis[i] = (1ll << 62ll);q.push(make_pair(0, 1));while (!q.empty()) {int u = q.top().second; q.pop();if (vis[u]) continue; vis[u] = 1;for (int i = head[u], v; i; i = e[i].next) if (dis[v = e[i].v] > dis[u] + e[i].w)dis[v] = dis[u] + e[i].w, q.push(make_pair(dis[v], v));}
}int main() {n = read(); rep(i, 1, n) a[i].x = read(), a[i].y = read(), a[i].id = i;sort(a + 1, a + 1 + n, cmpx); rep(i, 2, n) add(a[i].id, a[i - 1].id, a[i].x - a[i - 1].x);sort(a + 1, a + 1 + n, cmpy); rep(i, 2, n) add(a[i].id, a[i - 1].id, a[i].y - a[i - 1].y);dijkstra(); printf("%lld", dis[n]);return 0;
}

转载于:https://www.cnblogs.com/aziint/p/8495008.html

bzoj4152 [AMPPZ2014]The Captain相关推荐

  1. BZOJ4152 AMPPZ2014 The Captain(最短路)

    事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...

  2. bzoj4152: [AMPPZ2014]The Captain

    水... 这个建边排序一下从一边连向一边 dij在这种稀疏图果然不够优秀啊.只是学了一发. #include<cstdio> #include<iostream> #inclu ...

  3. 4152. [AMPPZ2014]The Captain(稠密图最短路)

    4152. [AMPPZ2014]The Captain 显然稠密图的边数时n2n^2n2量级,我们不可能把所有边建立出来,这时候通常寻求一些性质详细见[论题选编]稠密图最短路 针对本题我们可以先这样 ...

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

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MB Submit: 2107  Solved: 820 [Sub ...

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

    Time Limit: 20 Sec  Memory Limit: 256 MB Submit: 1550  Solved: 619 [Submit][Status][Discuss] Descrip ...

  6. 2019.4.summary

    2019.4.1 BZOJ1061: [Noi2008]志愿者招募 真心有点难QAQ https://www.byvoid.com/zhs/blog/noi-2008-employee 看void爷的 ...

  7. python画热力图_Python中绘制场景热力图

    原博文 2019-08-06 15:39 − 我们在做诸如人群密集度等可视化的时候,可能会考虑使用热力图,在Python中能很方便地绘制热力图. 下面以识别图片中的行人,并绘制热力图为例进行讲解. 步 ...

  8. 一句话题解(20170801~20170125)

    8.1 bzoj 4720 noip2016 换教室 floyd预处理+期望(薛定谔的猫) bzoj 4318 OSU! 三次函数期望值 从一次.二次推得 8.2 bzoj 1076 状压+期望DP ...

  9. bzoj4152 The Captain (dijkstra)

    做dijkstra,但只需要贪心地把每个点连到它左边.右边.上边.下面的第一个点就可以了 1 #include<bits/stdc++.h> 2 #define pa pair<in ...

最新文章

  1. [BZOJ3779]重组病毒(LCT+DFS序线段树)
  2. mysql 硬解析 软解析_ORACLE sql语句的硬解析与软解析(收藏)
  3. 你需要知道的requestAnimationFrame
  4. laravel oauth2.0 文件上传报错
  5. 部署gradle_使用Gradle消除开发重新部署
  6. 在JBoss AS 7上部署BroadleafCommerce 2.0
  7. 深入了解RabbitMQ工作原理及简单使用
  8. Python+OpenCV:Optical Flow(光流)
  9. 文献阅读High-throughput sequencing of the transcriptome and chromatin accessibility in the same cell
  10. 存储过程和存储函数和触发器示例
  11. CMake 使用总结(转载)
  12. FdfsConnectException:无法获取服务端资源:cant creat connection to /ip timeout
  13. Jupyter Notebook 数学公式
  14. Jenkins端口号修改
  15. 整理总结:零基础英语语法
  16. 固定IP地址及其重启后地址更改的解决方法
  17. 云计算的认识和看法_我的关于云计算的看法和认识
  18. Appium: Could not proxy command to the remote server. Original error: socket hang up
  19. SpringMVC jdbctemplate实现底层架构封装
  20. Python数据分析6——数据规整

热门文章

  1. 支付宝mysql集群_支付宝支撑2135亿成交额的数据库架构原理
  2. bootstarp怎么使盒子到最右边_江湖救急!盒子显示“很抱歉Launchercust 已停止运行” ?...
  3. SQL错误Duplicate column name 'NAME'名字重复应使用别名
  4. 谷歌Chrome浏览器开发者工具教程—基础功能篇
  5. 深度学习(Deep Learning)综述及算法简介
  6. php加密密码解析,php密码加密解密
  7. KNN(七)--最近邻及OpenCV源码分析
  8. JavaSE教程_1 安装jdk
  9. 安装webpack-cli时遇到的问题
  10. 安装node.jsvue3.0脚手架