description: You are given N points on an infinite plane with the Cartesian coordinate system on it. N-1 points lay on one line, and one point isn't on that line. You are on point K at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. What is the minimum length of the path? 
  
  Input
  
  The first line contains two integers: N (3 ≤ N ≤ 2*10^5) - the number of points, and K (1 ≤ K ≤ N) - the index of the starting point.
  
  Each of the next N lines contain two integers, A_i, B_i (-10^6 ≤ A_i, B_i ≤ 10^6) - coordinates of the i-th point.
  
  Output
  
  The output contains one number - the shortest path to visit all given points starting from point K. The absolute difference between your solution and the main solution shouldn't exceed 10^-6;
  
  Example
  
  Input
  
  
  5 2
  0 0
  -1 1
  2 -2
  0 1
  -2 2
  
  
  Output
  
  
  7.478709
  
  Note
  
  The shortest path consists of these moves: 
  
  2 -> 5 
  
  5 -> 4 
  
  4 -> 1 
  
  1 -> 3 
  
  There isn't any shorter path possible.
solution: #include <bits/stdc++.h>
  using namespace std;
  const long double eps = 1e-9;
  struct point {
    int x;
    int y;
    long double dist(const point &a) const { return hypot(a.x - x, a.y - y); }
  };
  bool operator<(const point &lhs, const point &rhs) {
    return make_tuple(lhs.x, lhs.y) < make_tuple(rhs.x, rhs.y);
  }
  bool operator==(const point &lhs, const point &rhs) {
    return make_tuple(lhs.x, lhs.y) == make_tuple(rhs.x, rhs.y);
  }
  void solve() {
    int n, k;
    cin >> n >> k;
    k--;
    vector<point> p(n);
    for (auto &i : p) {
      cin >> i.x >> i.y;
    }
    auto fixed = p[k];
    sort(p.begin(), p.end());
    map<int, int> Mcnt;
    for (int i = 0; i + 2 < n; ++i) {
      if (fabs(p[i].dist(p[i + 1]) + p[i + 2].dist(p[i + 1]) -
               p[i].dist(p[i + 2])) > eps) {
        for (int j = 0; j < 3; ++j) {
          Mcnt[i + j]++;
        }
      }
    }
    int mx = 0;
    for (auto e : Mcnt) {
      mx = max(mx, e.second);
    }
    switch (mx) {
      case 1: {
        if (Mcnt[0] == 1) {
          swap(p[0], p.back());
        }
        break;
      }
      case 2: {
        if (Mcnt[1] == 2) {
          swap(p[1], p.back());
        } else {
          swap(p[n - 2], p.back());
        }
        break;
      }
      case 3: {
        for (auto e : Mcnt) {
          if (e.second == 3) {
            swap(p.back(), p[e.first]);
            break;
          }
        }
        break;
      }
      default:
        break;
    }
    sort(p.begin(), p.begin() + n - 1);
    vector<long double> pref(n);
    pref[0] = 0;
    for (int i = 1; i < n; ++i) {
      pref[i] = pref[i - 1] + p[i - 1].dist(p[i]);
    }
    long double res = 1e9;
    if (p.back() == fixed) {
      res = pref[n - 2] + min(p.back().dist(p[0]), p.back().dist(p[n - 2]));
    } else {
      auto dist = [&](const int lhs, const int rhs) {
        return fabs(pref[rhs] - pref[lhs]);
      };
      for (int i = 0; i < n; i++) {
        if (p[i] == fixed) {
          k = i;
        }
      }
      for (int i = 0; i <= n - 2; ++i) {
        if (true) {
          long double v1 = dist(k, 0) + dist(0, i) + p[i].dist(p.back()) +
                           (i + 1 > n - 2 ? 0 : p[i + 1].dist(p.back())) +
                           (i + 1 > n - 1 ? 0 : dist(i + 1, n - 2));
          long double v2 = dist(k, i) + dist(0, i) + p[0].dist(p.back()) +
                           (i + 1 > n - 2 ? 0 : p[i + 1].dist(p.back())) +
                           (i + 1 > n - 1 ? 0 : dist(i + 1, n - 2));
          res = min(res, min(v1, v2));
        }
        if (true) {
          long double v1 = dist(k, n - 2) + dist(n - 2, i) + p[i].dist(p.back()) +
                           (i > 0 ? p[i - 1].dist(p.back()) : 0) +
                           (i > 1 ? dist(i - 1, 0) : 0);
          long double v2 = dist(k, i) + dist(n - 2, i) + p[n - 2].dist(p.back()) +
                           (i > 0 ? p[i - 1].dist(p.back()) : 0) +
                           (i > 1 ? dist(i - 1, 0) : 0);
          res = min(res, min(v1, v2));
        }
      }
    }
    cout << std::fixed << setprecision(10) << res << "\n";
  }
  int main() {
    solve();
    return 0;
  }
  