Advertisement
gertsog

I

Oct 17th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     #include <map>
  5.     #include <cmath>
  6.      
  7.     typedef std::pair<int, int> point2d;
  8.     typedef std::vector<int> iVec;
  9.      
  10.     std::vector<point2d> In;
  11.     std::map<point2d, int> dist;
  12.     std::map<int, int> min_e;
  13.     std::vector<bool> used;
  14.      
  15.     int len( const point2d & A, const point2d & B ) { return (A.first - B.first) * (A.first - B.first) + (A.second - B.second) * (A.second - B.second); }
  16.      
  17.     std::pair<int, int> find_min( int a )
  18.     {
  19.       int lenn, min_l = -1, min_ind = -1;
  20.       for (int b = 0; b < In.size(); b++)
  21.       {
  22.         if (a == b || used[b])
  23.           continue;
  24.      
  25.         lenn = (!dist[{a, b}]) ? (dist[{a, b}] = dist[{b, a}] = len(In[a], In[b])) : dist[{a, b}];
  26.         if ((lenn < min_l) || (min_l == -1))
  27.           min_l = lenn, min_ind = b;
  28.       }
  29.       min_e[a] = min_ind + 1;
  30.       return std::make_pair(min_ind, min_l);
  31.     }
  32.      
  33.     int main()
  34.     {
  35.       int n = 0;
  36.       scanf("%i", &n);
  37.       int x = 0, y = 0, i, tmp = 1;
  38.       long double res = 0;
  39.       std::pair<int, int> best_p;
  40.       used.resize(n);
  41.       std::generate(used.begin(), used.end(), []() { return false; });
  42.       int min_l = -1, min_ind = -1, lenn;
  43.      
  44.       // Prim
  45.       i = n;
  46.       while (i--)
  47.         scanf("%i%i", &x, &y), In.push_back(point2d(x, y));
  48.      
  49.       // fill the mst
  50.       used[0] = true;
  51.       for (int size = n - 1; size; size--)
  52.       {
  53.         min_l = min_ind = -1;
  54.         for (int a = 0; a < n; a++)
  55.         {
  56.           if (!used[a])
  57.             continue;
  58.      
  59.           tmp = min_e[a] - 1;
  60.      
  61.           if (!min_e[a] || used[min_e[a] - 1])
  62.             tmp = find_min(a).first;
  63.      
  64.           lenn = (!dist[{a, tmp}]) ? (dist[{a, tmp}] = dist[{tmp, a}] = len(In[a], In[tmp])) : dist[{a, tmp}];
  65.           if ((lenn < min_l) || (min_l == -1))
  66.             min_l = lenn, min_ind = tmp;
  67.         }
  68.      
  69.         if (min_ind == -1)
  70.         { printf("0");
  71.         return 0; }
  72.      
  73.         used[min_ind] = true;
  74.         res += sqrt(min_l);
  75.      
  76.         // recount if needed
  77.         for (int a = 0; a < n; a++)
  78.           if (used[a] && (!min_e[a] || used[min_e[a] - 1]))
  79.             find_min(a);
  80.       }
  81.      
  82.       //
  83.       printf("%lf", res);
  84.      
  85.       return 0;
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement