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. spring transaction 初识

    spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...

  2. Js常见算法实现汇总

    /*去重*/ <script> function delRepeat(arr){ var newArray=new Array(); var len=arr.length; for(var ...

  3. leetcode--3

    1. 题目: Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

  4. 日常入新坑,py一下

    首先是IDE,因为我经常在Ubuntu 18和win 10两个系统换来换去,所以IDE必须要能跨平台,所以这里就选了PyCharm.Py划重点—— 从Jet Brains的网站下载安装包,直接跟着默认 ...

  5. IOS UITextFieldDelegate (常用的代理方法)

    #pragma mark - UITextFieldDelegate // 返回NO代表着文本输入框不可以改变(不可以编辑) - (BOOL)textField:(UITextField *)text ...

  6. POJ2112 Optimal Milking---二分+Floyd+网络流

    题目链接: https://vjudge.net/problem/POJ-2112 题目大意: k个机器,每个机器最多服务m头牛. c头牛,每个牛需要1台机器来服务. 告诉你牛与机器每个之间的直接距离 ...

  7. 实现带查询功能的ComboBox控件

    实现效果: 知识运用: ComboBox控件的AutoCompleteMode属性 public AutoCompleteMode AutoCompleteMode{get;set;} //属性值为枚 ...

  8. 2018.6.18 MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案

    MyEclipse导入jQuery-1.8.0.min.js等文件的时候有时候会报了一堆missing semicolon的错误.怎么解决这个报错呢?方法如下: 1.选中报错的jquery文件例如&q ...

  9. python_47_Python2中字符编码与转码

    #python3默认是Unicode,Unicode是万国码,不管中文字符还是英文,所有的每个字符都占2个字节空间,16位 #python2默认是ascii码 #ascii码不能存中文,一个英文只能占 ...

  10. python_29_三级菜单

    menu={ '北京':{ '海淀':{ '五道口':{ '搜狐':{}, '网易':{}, 'Google':{}, }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, '优酷':{}, ...