AMPPZ2014 The Captain
传送门
Solution
比较妙,考虑按照给出的式子,只有\(x\)相邻或者\(y\)相邻的才会走,不然一定会走到相邻的再走\(x\)或\(y\),所以直接排序两边然后最短路即可。
Code
/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=400010;
struct point{
int x,y,id;
}p[N];
int n,front[N],cnt;
struct node{
int to,nxt,w;
}e[N<<1];
void Add(int u,int v,int w){e[++cnt]=(node){v,front[u],w};front[u]=cnt;}
bool cmpx(point a,point b){return a.x<b.x;}
bool cmpy(point a,point b){return a.y<b.y;}
typedef pair<int,int> pii;
#define mp make_pair
priority_queue<pii,vector<pii>,greater<pii> >q;
int dis[N],vis[N];
void dijkstra(){
memset(dis,63,sizeof(dis));dis[1]=0;q.push({0,1});
while(!q.empty()){
int u=q.top().second;q.pop();
if(vis[u])continue;vis[u]=1;
for(int i=front[u];i;i=e[i].nxt){
int v=e[i].to;
if(dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;q.push({dis[v],v});
}
}
}
}
int main(){
n=gi();
for(int i=1;i<=n;i++)p[i].x=gi(),p[i].y=gi(),p[i].id=i;
sort(p+1,p+n+1,cmpx);
for(int i=2;i<=n;i++){
Add(p[i].id,p[i-1].id,abs(p[i].x-p[i-1].x));
Add(p[i-1].id,p[i].id,abs(p[i].x-p[i-1].x));
}
sort(p+1,p+n+1,cmpy);
for(int i=2;i<=n;i++){
Add(p[i].id,p[i-1].id,abs(p[i].y-p[i-1].y));
Add(p[i-1].id,p[i].id,abs(p[i].y-p[i-1].y));
}
dijkstra();printf("%d\n",dis[n]);
return 0;
}
AMPPZ2014 The Captain的更多相关文章
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...
- 【BZOJ4152】[AMPPZ2014]The Captain 最短路
[BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...
- 4152: [AMPPZ2014]The Captain
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1561 Solved: 620[Submi ...
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
- bzoj4152 [AMPPZ2014]The Captain
最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...
- BZOJ4152:[AMPPZ2014]The Captain——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...
随机推荐
- MySQL5.6.17 绿色版 安装配置
安装篇: 下载完成之后,用解压工具解压到没有中文.空格的文件夹下,解压后的显示如图: 个人建议把解压后的文件夹重命名,如果有中文去掉中文,便于自己理解使用,如图: 打开重命名之后的文件夹,找到mysq ...
- 几分钟打造超级好看又好用的zsh command line环境
source: https://www.pexels.com/photo/office-working-app-computer-97077/ 注:这篇适用于用MAC 开发的developer 身为程 ...
- Asp.Net 技术
Asp.Net 技术Nuget 管理器Spring.NET 框架Spring.core 包 微服务Consul .NetCore 框架:Spring.net Nhibernate 框架Dapper.n ...
- tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...
- MFC中动态添加控件----寻找多年的秘籍,吐血推荐
原文作者tianwaik 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立 ...
- 15、vue项目封装axios并访问接口
1.在src下新建util文件夹,在util下新建request.js文件: 封装axios: import axios from 'axios' import QS from 'qs'; // im ...
- ubuntu升级python版本(3.5 -> 3.6)
#获取最新的python3.6,将其添加至当前apt库中,并自动导入公钥 $ sudo add-apt-repository ppa:jonathonf/python-3.6 $ sudo apt-g ...
- 一、DES加密和解密
一.DES加密和解密 原文:http://www.jb51.net/article/51879.htm 还有其他文章 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- VS code key shortcuts for windows
mac上的快捷键,尽量是选择像我用vs studio上靠近. ctrl+K+S: 显示快捷键列 ctrl+shift+p: 系统配置命令行 ctrl+p:项目中文件列表,选择文件 Alt+M:当前文件 ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...