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
|
#include <bits/stdc++.h> #define eps 1e-8 using namespace std; #define ms(a, b) memset((a), (b), sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; const int N = 1e4 + 5; const int M = 1e4 + 5; const int INF = 0x3f3f3f3f; const ll ll_max = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7;
inline ll read() { ll res = 0;bool f = 0;char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') f = 1;ch = getchar();} while (ch <= '9' && ch >= '0') {res = (res << 3) + (res << 1) + ch - '0';ch = getchar();} return f ? (~res + 1) : res; }
struct edge { int u, v, w, ne; } ed[M];
int vis[N], head[N], tim[N], dis[N]; int n, m, cnt;
void add(int u, int v, int w) { ed[cnt] = {u, v, w, head[u]}; head[u] = cnt++; }
void init() { memset(vis, 0, sizeof vis); memset(tim, 0, sizeof tim); memset(head, -1, sizeof head); memset(dis, INF, sizeof dis); cnt = 0; } void dijk(int s) { priority_queue<P, vector<P>, greater<P> > q; dis[s] = 0; q.emplace(0, s); while (!q.empty()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; ~i; i = ed[i].ne) { int v = ed[i].v, w = ed[i].w; if (dis[v] > dis[u] + w) dis[v] = dis[u] + w, q.emplace(dis[v], v); } } } struct point{ int x, y; }a[N], b[N]; bool isLine(point x, point y, point z){ if (z.x < min(x.x, y.x) || z.x > max(x.x, y.x) || z.y < min(x.y, y.y) || z.y > max(x.y, y.y)) return false; return (x.y - y.y) * (x.x - z.x) == (x.y - z.y) * (x.x - y.x); } bool is(int x, int y){ for (int i = 0; i < n; ++i) if (isLine(a[x], a[y], b[i])) return false; for (int i = 0; i < n; ++i){ if (i == x || y == i) continue; if (isLine(a[x], a[y], a[i])) return false; } return true; } int main(){ n = read(); init(); for (int i = 0; i < n; ++i) a[i].x = read(), a[i].y = read(); for (int i = 0; i < n; ++i) b[i].x = read(), b[i].y = read(); for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ if (i == j) continue; if (is(i, j)) add(i, j, 1); } } dijk(0); if (dis[n - 1] >= INF) dis[n - 1] = -1; cout << dis[n - 1] << "\n"; return 0; }
|