Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include <memory.h>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <map>
- #include <set>
- #include <iterator>
- #include <unordered_map>
- #include <unordered_set>
- #include <random>
- #include <chrono>
- //#define int long long
- #define iint long long
- #define pii pair<int, int>
- #define pb push_back
- #define all(vc) vc.begin(), vc.end()
- #define endl "\n"
- //#define double long double
- #define INF 10000000000000000009
- #define sp system("pause")
- using namespace std;
- const int N = 200009, R = 1 << 20, nlog = 19, ABC = 26, MOD = 1e9 + 7;
- iint dist(iint x0, iint y0, iint x1, iint y1, iint k)
- {
- if (x0 > k || x1 > k || y0 > k || y1 > k)
- {
- return -1;
- }
- if (x0 == 0 && y0 == 0)
- {
- if (x1 == 0 && y1 == 0)
- {
- return 0;
- }
- return -1;
- }
- if (x1 == 0 && y1 == 0)
- {
- return -1;
- }
- if (x0 == x1)
- {
- if (abs(y1) <= abs(x0) && abs(y0) <= abs(x0))
- {
- return abs(y0 - y1);
- }
- }
- if (y0 == y1)
- {
- if (abs(x0) <= abs(y0) && abs(x1) <= abs(y0))
- {
- return abs(x1 - x0);
- }
- }
- if (abs(x0) == abs(x1))
- {
- if (abs(y1) <= abs(x0) && abs(y0) <= abs(x0))
- {
- iint a = abs(-abs(x0) - min(y1, y0)) * 2 + abs(y1 - y0);
- iint b = abs(abs(x0) - max(y1, y0)) * 2 + abs(y1 - y0);
- return 2 * abs(x0) + min(a, b);
- }
- }
- if (abs(y0) == abs(y1))
- {
- if (abs(x0) <= abs(y0) && abs(x1) <= abs(y0))
- {
- iint a = abs(-abs(y0) - min(x1, x0)) * 2 + abs(x1 - x0);
- iint b = abs(abs(y0) - max(x1, x0)) * 2 + abs(x1 - x0);
- return 2 * abs(y0) + min(a, b);
- }
- }
- if (x0 == -y1 && abs(x0) >= abs(y0) && abs(x0) >= abs(x1))
- {
- return abs(y0 - y1) + abs(x1 - x0);
- }
- if (x1 == -y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
- {
- return abs(x1 - x0) + abs(y0 - y1);
- }
- if (x1 == y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
- {
- return abs(x1 - x0) + abs(y0 - y1);
- }
- swap(x1, x0);
- swap(y1, y0);
- if (x1 == y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
- {
- return abs(x1 - x0) + abs(y0 - y1);
- }
- return -1;
- }
- double rst(iint x0, iint y0, iint x1, iint y1)
- {
- return sqrt((x0 - x1) * (x0 - x1) + (y1 - y0) * (y1 - y0));
- }
- vector<pair<int, double>> gr[N];
- map<pii, int> mp;
- unordered_map<int, vector<pii>> num;
- set<pair<double, int>> q;
- vector<double> distt(N, INF);
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- cout.precision(17);
- int n, k;
- cin >> n >> k;
- if (n == 0)
- {
- int x0, y0, x1, y1;
- cin >> x0 >> y0 >> x1 >> y1;
- cout << dist(x0, y0, x1, y1, k);
- return 0;
- }
- int cur = 0;
- for (int i = 0; i < n; i++)
- {
- int a, b, c, d;
- cin >> a >> b >> c >> d;
- if (a > k || b > k || c > k || d > k)
- {
- continue;
- }
- if (mp.find({ a, b }) == mp.end())
- {
- mp[{a, b}] = cur;
- num[max(abs(a), abs(b))].push_back({ a,b });
- cur++;
- }
- if (mp.find({ c, d }) == mp.end())
- {
- mp[{c, d}] = cur;
- num[max(abs(c), abs(d))].pb({ c,d });
- cur++;
- }
- gr[mp[{a, b}]].push_back({ mp[{c,d}], rst(a,b,c,d) });
- gr[mp[{c, d}]].pb({ mp[{a, b}], rst(a, b, c, d) });
- }
- int a, b, c, d;
- cin >> a >> b >> c >> d;
- if (a > k || b > k || c > k || d > k)
- {
- cout << -1;
- return 0;
- }
- if (mp.find({ a,b }) == mp.end())
- {
- mp[{a, b}] = cur;
- num[max(abs(a), abs(b))].push_back({ a,b });
- cur++;
- }
- if (mp.find({ c,d }) == mp.end())
- {
- mp[{c, d}] = cur;
- num[max(abs(c), abs(d))].push_back({c,d });
- cur++;
- }
- num[max(abs(a), abs(b))].push_back({ a,b });
- num[max(abs(c), abs(d))].push_back({ c,d });
- for (auto it : num)
- {
- int cr = it.first;
- for (auto i = it.second.begin(); i != it.second.end(); i++)
- {
- for (auto ii = i; ii != it.second.end(); ii++)
- {
- int a = i->first;
- int b = i->second;
- int c = ii->first;
- int d = ii->second;
- gr[mp[{a, b}]].push_back({ mp[{c,d}], dist(a,b,c,d,k) });
- gr[mp[{c, d}]].pb({ mp[{a, b}], dist(a, b, c, d, k) });
- }
- }
- }
- int v = mp[{a, b}];
- int f = mp[{c, d}];
- distt[v] = 0;
- q.insert({ 0, v });
- while (!q.empty())
- {
- int u = q.begin()->second;
- q.erase(q.begin());
- for (auto ii : gr[u])
- {
- int i = ii.first;
- double w = ii.second;
- if (distt[i] > distt[u] + w)
- {
- q.erase({ distt[i], i });
- distt[i] = distt[u] + w;
- q.insert({ distt[i], i });
- }
- }
- }
- if (distt[f] == INF)
- {
- cout << -1;
- return 0;
- }
- cout << distt[f];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement