4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 1561  Solved: 620
[Submit][Status][Discuss]

Description

给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

Input

第一行包含一个正整数n(2<=n<=200000),表示点数。
接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。
 
 

Output

一个整数,即最小费用。

Sample Input

5
2 2
1 1
4 5
7 1
6 7

Sample Output

2
 

code

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define mp(a,b) make_pair(a,b)
#define pa pair<long long ,int>
using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
priority_queue< pa,vector<pa>,greater<pa> >q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void dij() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push(mp(,));
dis[] = ;
while (!q.empty()) {
pa x = q.top();q.pop();
int u = x.second;
if (vis[u]) continue;vis[u] = true;
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(mp(dis[v],v));
}
}
}
}
int main() {
freopen("1.txt","r",stdin);
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
dij();
printf("%d",dis[n]);
return ;
}

spfa被卡了QwQ

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
queue<int>q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void spfa() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push();
dis[] = ;
vis[] = true;
while (!q.empty()) {
int u = q.front();q.pop();
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;
if (!vis[v])q.push(v),vis[v] = true;
}
}
vis[u] = false;
}
}
int main() {
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
spfa();
printf("%d",dis[n]);
return ;
}
 
 

4152: [AMPPZ2014]The Captain的更多相关文章

  1. 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain

    循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...

  2. BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )

    先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...

  3. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2107  Solved: 820[Submi ...

  4. bzoj 4152[AMPPZ2014]The Captain

    bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...

  5. BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心

    Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...

  6. bzoj4152[AMPPZ2014]The Captain 最短路

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1517  Solved: 603[Submi ...

  7. BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*

    BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...

  8. 【BZOJ4152】[AMPPZ2014]The Captain 最短路

    [BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...

  9. BZOJ4152:[AMPPZ2014]The Captain——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...

随机推荐

  1. Java项目—嗖嗖移动业务大厅

    嗖嗖移动业务大厅包类(如下图): SosoMgr: package cn.biz; import java.util.Scanner; import cn.common.Common; import ...

  2. hibernate课程 初探单表映射2-1 hibernate进阶 本章简介

    本章简介,主要讲5大块的内容 1 hibernate.cfg.xml的配置 2 session 的简介 3 transaction的简介 4 session的详解 5 对象关系映射常用配置

  3. centos执行apt-get提示不存在

    在centos下用yum install xxx yum和apt-get的区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.De ...

  4. Django--对表的操作

    一丶多表创建 1.创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...

  5. 在浏览器地址栏按回车、F5、ctrl+F5刷新页面的区别

    url地址栏里敲击enter:这样的刷新,大家可以在firebug里看一下,只有少数的请求会发送出去,而且几乎没有图片的请求,这是因为请求时会先检查本地是不是缓存了请求的图片,如果有缓存而且没有过期( ...

  6. Js/Jquery获取input file的文件名

    html代码:     <input type="file" name="file" id="file" class="in ...

  7. 使用vuex管理数据

    src/vuex/store.js 组件里面使用引入store.js,注意路径 import store from 'store.js' 然后在使用的组件内data(){}同级放入store 三大常用 ...

  8. <logger>和<root>

    <logger>用来设置某一个包或者具体一个类的日志打印级别.以及制定<appender>.<logger>可以包含零个或者多个<appender-ref&g ...

  9. C#访问修改符

    修饰符可以指定访问的可见性,还可以指定其本质.(文章摘自<C#高级编程(第9版)>以及Microsoft) 1. 可见性修饰符 public:应用于所有类型或成员,即任何代码均可以访问该项 ...

  10. Java 中 Double 相关问题

    在项目当中,对于double类型数据的使用比较频繁.尤其是处理金钱相关的数据,在使用Double类型的数据时,涉及到精度,显示,四舍五入等等问题. 1.  显示问题,当double 数据 小于 0.0 ...