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号点的最小 ...
随机推荐
- URL(统一资源定位符)结构和注意事项
URL的常见结构: http://localhost/项目名称/文件1/文件2... 注意事项: 当我们在项目中在书写URL的时候,一般会出现两种情况: 第一种:在路径前面加上/,表示直接连在loca ...
- CSS3 使用自定义字体
CSS3 @font-face 规则 在 CSS3 之前,web 设计师必须使用已在用户计算机上安装好的字体.通过 CSS3,web 设计师可以使用他们喜欢的任意字体.当您您找到或购买到希望使用的字体 ...
- scroll事件实现监控滚动条并分页显示示例(zepto.js)
scroll事件实现监控滚动条并分页显示示例(zepto.js ) 需求:在APP落地页上的底部位置显示此前其他用户的购买记录,要求此div盒子只显示3条半,但一页有10条,div内的滑动条滑到一页 ...
- Sublime Text 3汉化中文版
Sublime Text 3汉化中文版是Sublime Text2的升级版.Sublime Text 是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Linux,Windows ...
- 对比MS Test与NUnit Test框架
前言: 项目中进行Unit Test时,肯定会用到框架,因为这样能够更快捷.方便的进行测试. .Net环境下的测试框架非常多,在这里只是对MS Test和NUnit Test进行一下比较, 因为这两个 ...
- ArcGisServer根据最大最小坐标换算瓦片行列号
1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或经纬度)的换算方法,而知道这个原理后,接下来我们要怎么用它呢?它和我们前端显示地图有什么关联呢?这一节,我会尽量详细的将这两个问题 ...
- [ACM] 1007 -球球方格
与兔子方格类似,不过一秒走一格: 输入 代码 #include<iostream> using namespace std; int main(void) { int test_count ...
- GitHub使用指南
文章地址:http://www.worldhello.net/gotgithub/index.html
- iOS启动过程
1.main函数 | 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 | 3.delega ...
- Mac平台与Windows平台下AndroidStudio增量升级
Android Studio增量升级什么情况下使用最合适呢? 比如现在的as版本是2.2版本,而你的as版本2.0版本,这个时候点Check For Updates就没有反应了,因为你已经2个有版本没 ...