define _CRT_SECURE_NO_WARNI NGS include cstdio include iostream includ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
using namespace std;
const int INF = 1000000000;
vector<vector<pair<int, int> > >g;
int X[200001] = { 0 },
Y[200001] = { 0 };
int main() {
freopen("input.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &X[i], Y[i]);
}
g.resize(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++i) {
if (i == j)
continue;
int dist = max(std::abs(X[i] - X[j]), std::abs(Y[i] - Y[j]));
g[i].push_back(make_pair(j, dist));
g[j].push_back(make_pair(i, dist));
}
}
int s = 0;
vector<int> d (n, INF), p (n);
d[s] = 0;
vector<char> u (n);
for (int i=0; i<n; ++i) {
int v = -1;
for (int j=0; j<n; ++j)
if (!u[j] && (v == -1 || d[j] < d[v]))
v = j;
if (d[v] == INF)
break;
u[v] = true;
for (size_t j=0; j<g[v].size(); ++j) {
int to = g[v][j].first,
len = g[v][j].second;
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = v;
}
}
}
cout <<d[n - 1] <<endl;
return 0;
}