4152: [AMPPZ2014]The Captain

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

n2建边肯定不行
min可以无视掉,建两条边
假如两点之间有第三个点,那么只要分别和第三个点建边就行了,这两点之间不需要
  • 所以每个点只需要向上下左右最靠近的点连边,排序即可
跑Dijkstra
  1. //
  2. // main.cpp
  3. // bzoj4152thecaptain
  4. //
  5. // Created by Candy on 9/7/16.
  6. // Copyright © 2016 Candy. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <algorithm>
  12. #include <cstring>
  13. #include <queue>
  14. using namespace std;
  15. const int N=,INF=1e9+;
  16. inline int read(){
  17. int x=,f=;char ch=getchar();
  18. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  19. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  20. return x*f;
  21. }
  22. int n;
  23. struct data{
  24. int x,y,id;
  25. }p[N];
  26. bool cmpx(data a,data b){
  27. return a.x<b.x;
  28. }
  29. bool cmpy(data a,data b){
  30. return a.y<b.y;
  31. }
  32.  
  33. struct edge{
  34. int v,w,ne;
  35. }e[N*];
  36. int h[N],cnt=;
  37. void ins(int u,int v,int w){
  38. cnt++;
  39. e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
  40. cnt++;
  41. e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
  42. }
  43.  
  44. void buildGraph(){
  45. sort(p+,p++n,cmpx);
  46. for(int i=;i<=n-;i++) ins(p[i].id,p[i+].id,p[i+].x-p[i].x);
  47. sort(p+,p++n,cmpy);
  48. for(int i=;i<=n-;i++) ins(p[i].id,p[i+].id,p[i+].y-p[i].y);
  49. }
  50.  
  51. struct hn{
  52. int u,d;
  53. bool operator <(const hn &rhs)const{return d>rhs.d;}
  54. };
  55. int d[N],done[N];
  56. priority_queue<hn> q;
  57. void dijkstra(int s){
  58. for(int i=;i<=n;i++) d[i]=INF;
  59. d[s]=;q.push((hn){s,});
  60. while(!q.empty()){
  61. hn x=q.top();q.pop();
  62. int u=x.u;
  63. if(done[u]) continue;
  64. done[u]=;
  65. for(int i=h[u];i;i=e[i].ne){
  66. int v=e[i].v;
  67. if(d[v]>d[u]+e[i].w){
  68. d[v]=d[u]+e[i].w;
  69. q.push((hn){v,d[v]});
  70. }
  71. }
  72. }
  73. }
  74. int main(int argc, const char * argv[]) {
  75. n=read();
  76. for(int i=;i<=n;i++) p[i].x=read(),p[i].y=read(),p[i].id=i;
  77. buildGraph();
  78. dijkstra();
  79. printf("%d",d[n]);
  80. return ;
  81. }
 

BZOJ4152The Captain[DIjkstra]的更多相关文章

  1. bzoj4152 The Captain (dijkstra)

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

  2. BZOJ4152 The Captain(dijkstra+巧妙建图)

    BZOJ4152 The Captain 题面很简洁: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 很明显 ...

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

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

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

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

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

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

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

    [题目大意] 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. [思路] 按照某维坐标排序,相邻两个点在这一维度 ...

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

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

  8. bzoj4152[AMPPZ2014]The Captain 最短路

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

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

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

随机推荐

  1. 使用js制作一般网站首页图片轮播效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 记录一下dotnetcore.1.0.0-VS2015Tools.preview2安装不上的问题

    错误提示:未指定错误 解决方案: 从这里下载:https://visualstudiogallery.msdn.microsoft.com/32f1fa1b-cdd5-4bd3-8f51-cd8f09 ...

  3. [android]亲自破解Flappy Bird(去广告+永生)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3544785.html  听说最近Flappy Bird很火,但 ...

  4. Android 4.4沉浸式状态栏的实现

    要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint) public clas ...

  5. swift 2.2 语法 (上)

    前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(中).swift 2.2语法(下) Swift与OC中常见的区别 导入框架 OC: ...

  6. Mac系统如何编辑hosts文件

    Mac系统如何编辑hosts文件 Hosts 是一个没有扩展名的系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系 统会首先 ...

  7. MVVM 模式下iOS项目目录结构详细说明

    ➠更多技术干货请戳:听云博客 我们在做项目的时候,会经常用到各种设计模式,最常见的要数 MVC (模型,视图,控制器)了.但是,今天我们要说的是另一种设计模式——MVVM. 所以 MVVM 到底是什么 ...

  8. 学习 java命令

    依稀记得自己第一次编译*.java文件,第一次运行*.class文件.但是六七年过去了,现在运行java写的程序更多的是用tomcat这种web容器.最近有个小需求,写一个监控zookeeper集群的 ...

  9. 更换mysql数据目录后出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法

    服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...

  10. One to One 的数据库模型设计与NHibernate配置

    在数据库模型设计中,最基本的实体关系有三种:一对一.一对多.多对多.关于一对多和多对多使用的情况较多,之前也有过一些讨论,现在来说明一下在数据库中一对一的模型设计. 首先,关系数据库中使用外键来表示一 ...