Advertisement
ke_timofeeva7

Untitled

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