【BZOJ4152】[AMPPZ2014]The Captain

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

题解:做法很神,也很简洁。

将所有点按x排序,显然只需要在相邻的点之间连边即可,然后在按y排序做一遍,跑最短路就完事了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <utility>
#define mp(A,B) make_pair(A,B)
using namespace std;
const int maxn=200010;
typedef long long ll;
struct node
{
int x,y,org;
}p[maxn];
int n,cnt;
int to[maxn<<2],next[maxn<<2],head[maxn],val[maxn<<2];
bool vis[maxn];
ll dis[maxn];
priority_queue<pair<ll,int> > q;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd()
{
int ret=0,f=1; char gc=nc();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=nc();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=nc();
return ret*f;
}
bool cmpx(const node &a,const node &b)
{
return a.x<b.x;
}
bool cmpy(const node &a,const node &b)
{
return a.y<b.y;
}
inline void add(int a,int b,int c)
{
to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
to[cnt]=a,val[cnt]=c,next[cnt]=head[b],head[b]=cnt++;
}
int main()
{
n=rd();
register int i,u;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++) p[i].x=rd(),p[i].y=rd(),p[i].org=i;
sort(p+1,p+n+1,cmpx);
for(i=1;i<n;i++) add(p[i].org,p[i+1].org,p[i+1].x-p[i].x);
sort(p+1,p+n+1,cmpy);
for(i=1;i<n;i++) add(p[i].org,p[i+1].org,p[i+1].y-p[i].y);
memset(dis,0x3f,sizeof(dis));
dis[1]=0,q.push(mp(0,1));
while(!q.empty())
{
u=q.top().second,q.pop();
if(vis[u]) continue;
if(u==n)
{
printf("%lld\n",dis[n]);
return 0;
}
vis[u]=1;
for(i=head[u];i!=-1;i=next[i]) if(dis[to[i]]>dis[u]+val[i])
dis[to[i]]=dis[u]+val[i],q.push(mp(-dis[to[i]],to[i]));
}
}

【BZOJ4152】[AMPPZ2014]The Captain 最短路的更多相关文章

  1. bzoj4152[AMPPZ2014]The Captain 最短路

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

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

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

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

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

  4. BZOJ4152 AMPPZ2014 The Captain(最短路)

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

  5. bzoj4152 [AMPPZ2014]The Captain

    最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...

  6. 【BZOJ4152】The Captain(最短路)

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

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

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

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

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

  9. bzoj 4152[AMPPZ2014]The Captain

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

随机推荐

  1. C语言之指针基础概念

    今天就写一下关于C语言指针的一些感想吧. 很多同学都搞不懂指针,我一开始也云里雾里没看懂指针,而且老师又把指针说得很难的样子.其实主要是把指针”*“的作用给弄混了,不用畏惧,细心点看就可以了. 首先简 ...

  2. php_memcahed telnet远程操作方法

    一.存储命令 存储命令的格式: <command name> <key> <flags> <exptime> <bytes> <dat ...

  3. 快速理解linux流编辑器sed命令

    原创 杜亦舒性能与架构 之前介绍过 awk 命令,sed 命令同样是非常重要的文本处理工具,涉及到linux shell开发时,几乎是避不开这两大利器的 sed 是 stream editor 的简写 ...

  4. XP win2003系统 微软雅黑字体的使用方法

    微软雅黑是微软公司为其新一代操作系统Vista开发的中文字体,据说它将是迄今为止个人电脑上可以显示的最清晰的中文字体.       微软公司表示,在新一代操作系统中为了能够更加清晰的显示文字,目前正在 ...

  5. UINavigationController 返回按钮去掉文字

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetric ...

  6. [css]后台管理系统布局

    知识点: 绝对定位+overflowhidden 整体思路 三大块 pg-header---需要固定 (height:48px) pg-content menu 右侧菜单-需要固定(width:200 ...

  7. 谷歌上不去了,长久解决方式。能够稳定高速上Google和Gmail

    稳定上Google的神器 国内Google很不稳定.速度慢且常常上不去,通过"我要上Google".能够安全稳定地使用Google.Gmail.Google+等平时须要特殊手段才干 ...

  8. [转]AngularJS ui-router (嵌套路由)

    本文转自:http://www.oschina.net/translate/angularjs-ui-router-nested-routes http://www.codeproject.com/A ...

  9. C运行库和VC对应关系

    ## C运行库和VC对应关系----------------------------------------------------------------Msvcr60.DLL -- VC6Msvc ...

  10. Java 调用 C/C++ 之 JNA 系列实战篇 —— 输出char * (六)

    一. 工作环境 1. windows (64位), JDK (64位),dll文件 (64位) 2. Linux (64位),      JDK (64位),so文件 (64位) 3. JNA的官方资 ...