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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <iostream> #include <vector> #include <queue> #include <bitset> #include <algorithm> #include <cmath> using namespace std;
struct Node { int id = -1; int d = -1; vector<int> neighbors; vector<int> edges; };
struct NodeCmp { bool operator()(const Node &a, const Node &b) const { return b.d < a.d; } };
int dijkstra(Node* graph, int N, int s, int t) { bitset<100001> set; set.reset(); priority_queue<Node, vector<Node>, NodeCmp> heap; graph[s].d = 0; set[s] = 1; for (int i = 0; i < graph[s].neighbors.size(); i++) { graph[graph[s].neighbors[i]].d = graph[s].edges[i]; heap.push(graph[graph[s].neighbors[i]]); }
int i = 0; while (i < N - 1) { Node nearestNode = heap.top(); heap.pop(); if (set[nearestNode.id]) continue; i++; set[nearestNode.id] = 1; for (int j = 0; j < nearestNode.neighbors.size(); j++) { if (graph[nearestNode.neighbors[j]].d == -1 || nearestNode.edges[j] + nearestNode.d < graph[nearestNode.neighbors[j]].d) graph[nearestNode.neighbors[j]].d = nearestNode.edges[j] + nearestNode.d; heap.push(graph[nearestNode.neighbors[j]]); } }
return graph[t].d; }
struct Point { int id = 0; int x = 0; int y = 0; Point(int i, int xv, int yv) { id = i; x = xv; y = yv; } };
bool xComp(const Point &a, const Point &b) { return a.x < b.x; }
bool yComp(const Point &a, const Point &b) { return a.y < b.y; }
int main() { int N = 0; cin >> N; vector<Point> coord; for (int i = 1; i < N + 1; i++) { int x = 0, y = 0; cin >> x >> y; coord.push_back(Point(i, x, y)); } Node* graph = new Node[N + 1]; for (int i = 0; i < N + 1; i++) graph[i].id = i; sort(coord.begin(), coord.end(), xComp); for (int i = 0; i < N - 1; i++) { int a = coord[i].id; int b = coord[i + 1].id; int edge = min(abs(coord[i].x - coord[i + 1].x), abs(coord[i].y - coord[i + 1].y)); graph[a].neighbors.push_back(b); graph[a].edges.push_back(edge); graph[b].neighbors.push_back(a); graph[b].edges.push_back(edge); } sort(coord.begin(), coord.end(), yComp); for (int i = 0; i < N - 1; i++) { int a = coord[i].id; int b = coord[i + 1].id; int edge = min(abs(coord[i].x - coord[i + 1].x), abs(coord[i].y - coord[i + 1].y)); graph[a].neighbors.push_back(b); graph[a].edges.push_back(edge); graph[b].neighbors.push_back(a); graph[b].edges.push_back(edge); }
cout << dijkstra(graph, N, 1, N) << endl;
return 0; }
|