【BZOJ4152】[AMPPZ2014]The Captain 最短路
【BZOJ4152】[AMPPZ2014]The Captain
Description
Input
Output
Sample Input
2 2
1 1
4 5
7 1
6 7
Sample Output
题解:做法很神,也很简洁。
将所有点按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 最短路的更多相关文章
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- BZOJ4152 AMPPZ2014 The Captain(最短路)
事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...
- bzoj4152 [AMPPZ2014]The Captain
最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...
- 【BZOJ4152】The Captain(最短路)
[BZOJ4152]The Captain(最短路) 题面 BZOJ Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求 ...
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
随机推荐
- HTML-HTML5+CSS3权威指南阅读(二、让IE支持HTML5标签及对CSS3 Media Query的兼容)
兼容解决 HTML5是在低版本的浏览器(IE8-)的兼容是有限的,类似很多结构性的标 签<header>.<section>.<footer>等在IE8以下不会被识 ...
- OGNL表达式语言网上转来的
1.概念:OGNL是ObjectGraphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语言. 2.功能: ...
- elem.attr()无法正确判断checkbox是否选中
本篇文章由:http://xinpure.com/elem-attr-does-not-correctly-determine-whether-the-checkbox-is-selected/ 关于 ...
- Layui 弹出层组件——layer
layer是作为Layui[经典模块化前端框架]的一个弹层模块,由于其用户基数较大,所以把layer作为独立组件来维护. 基础参数: 基础参数主要指调用方法时用到的配置项,如:layer.open({ ...
- 中小型研发团队架构实践:任务调度Job
一.Job 简介 Job 类似于数据库中的作业,多用于实现定时执行任务.适用场景主要包括定时轮询数据库同步.定时处理数据.定时邮件通知等. 我们的 Job 分为操作系统级别定时任务 WinJob 和 ...
- c# 按位与,按位或
在工作中遇到按位或组合权限串.一直不是特别明白.今天终于花了半个下午的时间搞明白其中的道理. 首先每一个权限数都是2的N次方数 如:k1=2 ; //添加 k2=4 ; //删除 k3=8; //修改 ...
- js 时间对比
https://www.cnblogs.com/xiangsj/p/7977325.html http://www.jb51.net/article/45560.htm isOverdue (time ...
- Atitit.http连接合并组件 ConnReducerV3 新特性
Atitit.http连接合并组件 ConnReducerV3 新特性 D:\0workspace\AtiPlatf_cms\src\com\attilax\util\ConnReducerV2. ...
- C# 正则表达式替换制定关键词后面的所有内容
如题,将 {OUTSCIPTE} 关键词后的所有内容替换为string.Empty(包含关键字)这个正则该怎么写?我是 {OUTSCIPTE}(.*)$ 写的但是什么反应也没有 string str= ...
- MooseFS基础和安装
一.MooseFS简介 1.介绍 MooseFS是一个具备冗余容错功能的分布式网络文件系统,它将数据分别存放在多个物理服务器单独磁盘或分区上,确保一份数据有多个备份副本.对于访问的客户端或者用户来说, ...