Sol

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 5);
typedef int Arr[_];

IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}

Arr first, dis, vis;
int n, m, cnt;
struct Point{
int x, y, id;
} p[_];
struct Data{
int u, dis;

IL int operator <(RG Data B) const{
return dis > B.dis;
}
};
priority_queue <Data> Q;
struct Edge{
int to, next, w;
} edge[_ << 2];

IL void Add(RG int u, RG int v, RG int w){
edge[cnt] = (Edge){v, first[u], w}, first[u] = cnt++;
edge[cnt] = (Edge){u, first[v], w}, first[v] = cnt++;
}

IL int Cmp1(RG Point A, RG Point B){
return A.x < B.x;
}

IL int Cmp2(RG Point A, RG Point B){
return A.y < B.y;
}

IL void Dijkstra(){
Fill(dis, 63), dis[1] = 0, Q.push((Data){1, 0});
while(!Q.empty()){
RG Data x = Q.top(); Q.pop();
if(vis[x.u]) continue;
vis[x.u] = 1;
for(RG int e = first[x.u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(dis[x.u] + w < dis[v]) Q.push((Data){v, dis[v] = dis[x.u] + w});
}
}
}

int main(RG int argc, RG char *argv[]){
n = Input();
for(RG int i = 1; i <= n; ++i)
first[i] = -1, p[i] = (Point){Input(), Input(), i};
sort(p + 1, p + n + 1, Cmp1);
for(RG int i = 1; i < n; ++i)
Add(p[i].id, p[i + 1].id, p[i + 1].x - p[i].x);
sort(p + 1, p + n + 1, Cmp2);
for(RG int i = 1; i < n; ++i)
Add(p[i].id, p[i + 1].id, p[i + 1].y - p[i].y);
Dijkstra();
printf("%d\n", dis[n]);
return 0;
}

posted @ 2018-03-28 13:20  Cyhlnj  阅读(98)  评论(0编辑  收藏  举报