BZOJ4152The Captain[DIjkstra]
4152: [AMPPZ2014]The Captain
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 700 Solved: 266
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 2
1 1
4 5
7 1
6 7
Sample Output
HINT
Source
- 所以每个点只需要向上下左右最靠近的点连边,排序即可
- //
- // main.cpp
- // bzoj4152thecaptain
- //
- // Created by Candy on 9/7/16.
- // Copyright © 2016 Candy. All rights reserved.
- //
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- using namespace std;
- const int N=,INF=1e9+;
- inline int read(){
- int x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int n;
- struct data{
- int x,y,id;
- }p[N];
- bool cmpx(data a,data b){
- return a.x<b.x;
- }
- bool cmpy(data a,data b){
- return a.y<b.y;
- }
- struct edge{
- int v,w,ne;
- }e[N*];
- int h[N],cnt=;
- void ins(int u,int v,int w){
- cnt++;
- e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
- cnt++;
- e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
- }
- void buildGraph(){
- sort(p+,p++n,cmpx);
- for(int i=;i<=n-;i++) ins(p[i].id,p[i+].id,p[i+].x-p[i].x);
- sort(p+,p++n,cmpy);
- for(int i=;i<=n-;i++) ins(p[i].id,p[i+].id,p[i+].y-p[i].y);
- }
- struct hn{
- int u,d;
- bool operator <(const hn &rhs)const{return d>rhs.d;}
- };
- int d[N],done[N];
- priority_queue<hn> q;
- void dijkstra(int s){
- for(int i=;i<=n;i++) d[i]=INF;
- d[s]=;q.push((hn){s,});
- while(!q.empty()){
- hn x=q.top();q.pop();
- int u=x.u;
- if(done[u]) continue;
- done[u]=;
- for(int i=h[u];i;i=e[i].ne){
- int v=e[i].v;
- if(d[v]>d[u]+e[i].w){
- d[v]=d[u]+e[i].w;
- q.push((hn){v,d[v]});
- }
- }
- }
- }
- int main(int argc, const char * argv[]) {
- n=read();
- for(int i=;i<=n;i++) p[i].x=read(),p[i].y=read(),p[i].id=i;
- buildGraph();
- dijkstra();
- printf("%d",d[n]);
- return ;
- }
BZOJ4152The Captain[DIjkstra]的更多相关文章
- bzoj4152 The Captain (dijkstra)
做dijkstra,但只需要贪心地把每个点连到它左边.右边.上边.下面的第一个点就可以了 #include<bits/stdc++.h> #define pa pair<int,in ...
- BZOJ4152 The Captain(dijkstra+巧妙建图)
BZOJ4152 The Captain 题面很简洁: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 很明显 ...
- BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心
Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- 【bzoj4152】[AMPPZ2014]The Captain 堆优化Dijkstra
题目描述 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入 第一行包含一个正整数n(2<=n< ...
- 【堆优化Dijkstra】BZOJ4152- [AMPPZ2014]The Captain
[题目大意] 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. [思路] 按照某维坐标排序,相邻两个点在这一维度 ...
- 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 ...
- 『The Captain 最短路建图优化』
The Captain(BZOJ 4152) Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小 ...
随机推荐
- 使用js制作一般网站首页图片轮播效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 记录一下dotnetcore.1.0.0-VS2015Tools.preview2安装不上的问题
错误提示:未指定错误 解决方案: 从这里下载:https://visualstudiogallery.msdn.microsoft.com/32f1fa1b-cdd5-4bd3-8f51-cd8f09 ...
- [android]亲自破解Flappy Bird(去广告+永生)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3544785.html 听说最近Flappy Bird很火,但 ...
- Android 4.4沉浸式状态栏的实现
要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint) public clas ...
- swift 2.2 语法 (上)
前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(中).swift 2.2语法(下) Swift与OC中常见的区别 导入框架 OC: ...
- Mac系统如何编辑hosts文件
Mac系统如何编辑hosts文件 Hosts 是一个没有扩展名的系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系 统会首先 ...
- MVVM 模式下iOS项目目录结构详细说明
➠更多技术干货请戳:听云博客 我们在做项目的时候,会经常用到各种设计模式,最常见的要数 MVC (模型,视图,控制器)了.但是,今天我们要说的是另一种设计模式——MVVM. 所以 MVVM 到底是什么 ...
- 学习 java命令
依稀记得自己第一次编译*.java文件,第一次运行*.class文件.但是六七年过去了,现在运行java写的程序更多的是用tomcat这种web容器.最近有个小需求,写一个监控zookeeper集群的 ...
- 更换mysql数据目录后出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法
服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...
- One to One 的数据库模型设计与NHibernate配置
在数据库模型设计中,最基本的实体关系有三种:一对一.一对多.多对多.关于一对多和多对多使用的情况较多,之前也有过一些讨论,现在来说明一下在数据库中一对一的模型设计. 首先,关系数据库中使用外键来表示一 ...