BZOJ4152:[AMPPZ2014]The Captain——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4152
给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。
先以纵坐标从下往上不考虑横坐标为例,发现我们付出代价一定是最近的两行之间的点的代价,于是对y排序,则相邻两个点连边即可。
横坐标同理。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second;
const int N=2e5+;
const int M=N*;
const int INF=2e9;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int x,y,id;
}p[N];
struct edge{
int to,nxt,w;
}e[M];
int n,cnt,head[N],dis[N];
priority_queue<pii,vector<pii>,greater<pii> >q;
inline bool cmpx(node a,node b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline bool cmpy(node a,node b){
return a.y==b.y?a.x<b.x:a.y<b.y;
}
inline void add(int u,int v,int w){
e[++cnt].to=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;
}
inline int len(node a,node b){
return min(abs(a.x-b.x),abs(a.y-b.y));
}
void dij(int s){
for(int i=;i<=n;i++)dis[i]=INF;
dis[s]=;q.push(pii(,s));
while(!q.empty()){
int u=q.top().se;int f=q.top().fi;q.pop();
if(f!=dis[u])continue;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
q.push(pii(dis[v],v));
}
}
}
}
int main(){
n=read();
for(int i=;i<=n;i++){
p[i].x=read(),p[i].y=read();
p[i].id=i;
}
sort(p+,p+n+,cmpx);
for(int i=;i<=n;i++){
int u=p[i-].id,v=p[i].id,w=len(p[i-],p[i]);
add(u,v,w);add(v,u,w);
}
sort(p+,p+n+,cmpy);
for(int i=;i<=n;i++){
int u=p[i-].id,v=p[i].id,w=len(p[i-],p[i]);
add(u,v,w);add(v,u,w);
}
dij();
printf("%d\n",dis[n]);
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++
BZOJ4152:[AMPPZ2014]The Captain——题解的更多相关文章
- BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- BZOJ4152 AMPPZ2014 The Captain(最短路)
事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...
- bzoj4152 [AMPPZ2014]The Captain
最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...
- 【BZOJ4152】[AMPPZ2014]The Captain 最短路
[BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...
- 【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( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
随机推荐
- lua 中的 loadfile、dofile和require的调用
文件 hello.lua print("hello") function say() print("hello world") end 1. 介绍: dofil ...
- 04-容器 What, Why, How
What - 什么是容器? 容器是一种轻量级.可移植.自包含的软件打包技术,使应用程序可以在几乎任何地方以相同的方式运行.开发人员在自己笔记本上创建并测试好的容器,无需任何修改就能够在生产系统的虚拟机 ...
- MYSQL存储过程调试过程
mysql不像oracle有plsqldevelper工具用来调试存储过程,所以有几种简单的方式追踪执行过程: 1.用一张临时表,记录调试过程: 2.直接在存储过程中,增加select xxx,在控 ...
- Testing Harbor REST API on Swagger
先贴官方地址,我的做法差不多 https://github.com/goharbor/harbor/blob/master/docs/configure_swagger.md 1.下载对应资源 wge ...
- AndroidStudio引入AAR依赖
title: AndroidStudio引入AAR依赖 date: 2016-08-10 00:25:57 tags: [aar] categories: [Tool,Gradle] --- 概述 本 ...
- Python Fileinput 模块介绍
作者博文地址:http://www.cnblogs.com/spiritman/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...
- [leetcode-884-Uncommon Words from Two Sentences]
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树
题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...
- 后端编程语言PHP
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.PHP 简介 PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言. PHP 脚本在服务器上执行. 什么是 PHP?(超文本预处理器 ...
- 作业MathExamV2.0
MathExam233 211614269 林凯 211601233张康凌 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时( ...