Advertisement
ke_timofeeva7

ебанина

Jan 13th, 2022
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7. #include <iomanip>
  8. #include <memory.h>
  9. #include <queue>
  10. #include <deque>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <iterator>
  15. #include <unordered_map>
  16. #include <unordered_set>
  17. #include <random>
  18. #include <chrono>
  19. //#define int long long
  20. #define iint long long
  21. #define pii pair<int, int>
  22. #define pb push_back
  23. #define all(vc) vc.begin(), vc.end()
  24. #define endl "\n"
  25. //#define double long double
  26. #define INF 10000000000000000009
  27. #define sp system("pause")
  28. using namespace std;
  29.  
  30. const int N = 200009, R = 1 << 20, nlog = 19, ABC = 26, MOD = 1e9 + 7;
  31.  
  32. iint dist(iint x0, iint y0, iint x1, iint y1, iint k)
  33. {
  34.     if (x0 > k || x1 > k || y0 > k || y1 > k)
  35.     {
  36.         return -1;
  37.     }
  38.  
  39.     if (x0 == 0 && y0 == 0)
  40.     {
  41.         if (x1 == 0 && y1 == 0)
  42.         {
  43.             return 0;
  44.         }
  45.  
  46.         return -1;
  47.     }
  48.  
  49.     if (x1 == 0 && y1 == 0)
  50.     {
  51.         return -1;
  52.     }
  53.  
  54.     if (x0 == x1)
  55.     {
  56.         if (abs(y1) <= abs(x0) && abs(y0) <= abs(x0))
  57.         {
  58.             return abs(y0 - y1);
  59.         }
  60.     }
  61.  
  62.     if (y0 == y1)
  63.     {
  64.         if (abs(x0) <= abs(y0) && abs(x1) <= abs(y0))
  65.         {
  66.             return abs(x1 - x0);
  67.         }
  68.     }
  69.  
  70.     if (abs(x0) == abs(x1))
  71.     {
  72.         if (abs(y1) <= abs(x0) && abs(y0) <= abs(x0))
  73.         {
  74.             iint a = abs(-abs(x0) - min(y1, y0)) * 2 + abs(y1 - y0);
  75.             iint b = abs(abs(x0) - max(y1, y0)) * 2 + abs(y1 - y0);
  76.             return 2 * abs(x0) + min(a, b);
  77.         }
  78.     }
  79.  
  80.     if (abs(y0) == abs(y1))
  81.     {
  82.         if (abs(x0) <= abs(y0) && abs(x1) <= abs(y0))
  83.         {
  84.             iint a = abs(-abs(y0) - min(x1, x0)) * 2 + abs(x1 - x0);
  85.             iint b = abs(abs(y0) - max(x1, x0)) * 2 + abs(x1 - x0);
  86.             return 2 * abs(y0) + min(a, b);
  87.         }
  88.     }
  89.  
  90.     if (x0 == -y1 && abs(x0) >= abs(y0) && abs(x0) >= abs(x1))
  91.     {
  92.         return abs(y0 - y1) + abs(x1 - x0);
  93.     }
  94.  
  95.     if (x1 == -y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
  96.     {
  97.         return abs(x1 - x0) + abs(y0 - y1);
  98.     }
  99.  
  100.     if (x1 == y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
  101.     {
  102.         return abs(x1 - x0) + abs(y0 - y1);
  103.     }
  104.  
  105.     swap(x1, x0);
  106.     swap(y1, y0);
  107.  
  108.     if (x1 == y0 && abs(x1) >= abs(y1) && abs(x1) >= abs(x0))
  109.     {
  110.         return abs(x1 - x0) + abs(y0 - y1);
  111.     }
  112.  
  113.     return -1;
  114. }
  115.  
  116. double rst(iint x0, iint y0, iint x1, iint y1)
  117. {
  118.     return sqrt((x0 - x1) * (x0 - x1) + (y1 - y0) * (y1 - y0));
  119. }
  120.  
  121. vector<pair<int, double>> gr[N];
  122. map<pii, int> mp;
  123. unordered_map<int, vector<pii>> num;
  124. set<pair<double, int>> q;
  125. vector<double> distt(N, INF);
  126.  
  127. signed main()
  128. {
  129.     ios_base::sync_with_stdio(false);
  130.     cin.tie(0);
  131.     cout.tie(0);
  132.     cout.precision(17);
  133.  
  134.     int n, k;
  135.     cin >> n >> k;
  136.  
  137.     if (n == 0)
  138.     {
  139.         int x0, y0, x1, y1;
  140.         cin >> x0 >> y0 >> x1 >> y1;
  141.  
  142.         cout << dist(x0, y0, x1, y1, k);
  143.         return 0;
  144.     }
  145.  
  146.  
  147.     int cur = 0;
  148.  
  149.     for (int i = 0; i < n; i++)
  150.     {
  151.         int a, b, c, d;
  152.         cin >> a >> b >> c >> d;
  153.  
  154.         if (a > k || b > k || c > k || d > k)
  155.         {
  156.             continue;
  157.         }
  158.  
  159.         if (mp.find({ a, b }) == mp.end())
  160.         {
  161.             mp[{a, b}] = cur;
  162.             num[max(abs(a), abs(b))].push_back({ a,b });
  163.             cur++;
  164.         }
  165.  
  166.         if (mp.find({ c, d }) == mp.end())
  167.         {
  168.             mp[{c, d}] = cur;
  169.             num[max(abs(c), abs(d))].pb({ c,d });
  170.             cur++;
  171.         }
  172.  
  173.         gr[mp[{a, b}]].push_back({ mp[{c,d}], rst(a,b,c,d) });
  174.         gr[mp[{c, d}]].pb({ mp[{a, b}], rst(a, b, c, d) });
  175.     }
  176.  
  177.     int a, b, c, d;
  178.     cin >> a >> b >> c >> d;
  179.  
  180.     if (a > k || b > k || c > k || d > k)
  181.     {
  182.         cout << -1;
  183.         return 0;
  184.     }
  185.  
  186.     if (mp.find({ a,b }) == mp.end())
  187.     {
  188.         mp[{a, b}] = cur;
  189.         num[max(abs(a), abs(b))].push_back({ a,b });
  190.         cur++;
  191.     }
  192.  
  193.     if (mp.find({ c,d }) == mp.end())
  194.     {
  195.         mp[{c, d}] = cur;
  196.         num[max(abs(c), abs(d))].push_back({c,d });
  197.         cur++;
  198.     }
  199.  
  200.     num[max(abs(a), abs(b))].push_back({ a,b });
  201.     num[max(abs(c), abs(d))].push_back({ c,d });
  202.  
  203.     for (auto it : num)
  204.     {
  205.         int cr = it.first;
  206.  
  207.         for (auto i = it.second.begin(); i != it.second.end(); i++)
  208.         {
  209.             for (auto ii = i; ii != it.second.end(); ii++)
  210.             {
  211.                 int a = i->first;
  212.                 int b = i->second;
  213.                 int c = ii->first;
  214.                 int d = ii->second;            
  215.  
  216.                 gr[mp[{a, b}]].push_back({ mp[{c,d}], dist(a,b,c,d,k) });
  217.                 gr[mp[{c, d}]].pb({ mp[{a, b}], dist(a, b, c, d, k) });
  218.             }
  219.         }
  220.     }
  221.  
  222.     int v = mp[{a, b}];
  223.     int f = mp[{c, d}];
  224.  
  225.     distt[v] = 0;
  226.  
  227.     q.insert({ 0, v });
  228.  
  229.     while (!q.empty())
  230.     {
  231.         int u = q.begin()->second;
  232.         q.erase(q.begin());
  233.  
  234.         for (auto ii : gr[u])
  235.         {
  236.             int i = ii.first;
  237.             double w = ii.second;
  238.  
  239.             if (distt[i] > distt[u] + w)
  240.             {
  241.                 q.erase({ distt[i], i });
  242.  
  243.                 distt[i] = distt[u] + w;
  244.                 q.insert({ distt[i], i });
  245.             }
  246.         }
  247.     }
  248.  
  249.     if (distt[f] == INF)
  250.     {
  251.         cout << -1;
  252.         return 0;
  253.     }
  254.  
  255.     cout << distt[f];
  256.     return 0;
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement