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 int Inf = 0x3f3f3f3f;
  int inline gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
  int N, K;
  int a[200005], b[200005];
  map<pair<int, int>, vector<int> > ps;
  int id[200005];
  double dist(int x, int y) {
    return sqrt(1ll * (a[x] - a[y]) * (a[x] - a[y]) +
                1ll * (b[x] - b[y]) * (b[x] - b[y]));
  }
  int main() {
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; ++i) scanf("%d%d", a + i, b + i);
    for (int i = 2; i <= N; ++i) {
      int x = a[i] - a[1], y = b[i] - b[1];
      int g = gcd(x, y);
      x /= g;
      y /= g;
      if (y < 0 || (y == 0 && x < 0)) x *= -1, y *= -1;
      ps[make_pair(x, y)].push_back(i);
    }
    int kp;
    if (((int)(ps).size()) == 1)
      kp = 1;
    else if (((int)(ps).size()) == 2) {
      auto it = ps.begin();
      if (((int)(it->second).size()) == 1)
        kp = it->second[0];
      else
        kp = (++it)->second[0];
    } else
      kp = 1;
    int ids = 0;
    for (int i = 1; i <= N; ++i)
      if (kp != i) id[++ids] = i;
    sort(id + 1, id + N, [&](int x, int y) {
      return make_pair(a[x], b[x]) < make_pair(a[y], b[y]);
    });
    double ans;
    if (K == kp) {
      ans = dist(kp, id[1]) + dist(id[1], id[N - 1]);
      ans = min(ans, dist(kp, id[N - 1]) + dist(id[1], id[N - 1]));
    } else {
      int nid = 1;
      while (id[nid] != K) ++nid;
      ans = dist(K, id[1]) + dist(id[1], kp) +
            (nid == N - 1 ? 0
                          : dist(kp, id[nid + 1]) + dist(id[nid + 1], id[N - 1]));
      ans = min(
          ans,
          dist(K, id[N - 1]) + dist(id[N - 1], kp) +
              (nid == 1 ? 0 : dist(kp, id[nid - 1]) + dist(id[nid - 1], id[1])));
      ans = min(ans, dist(K, id[1]) + dist(id[1], kp) +
                         (nid == N - 1 ? 0
                                       : dist(kp, id[N - 1]) +
                                             dist(id[nid + 1], id[N - 1])));
      ans = min(ans,
                dist(K, id[N - 1]) + dist(id[N - 1], kp) +
                    (nid == 1 ? 0 : dist(kp, id[1]) + dist(id[nid - 1], id[1])));
      ans = min(ans, dist(K, kp) + dist(kp, id[1]) + dist(id[1], id[N - 1]));
      ans = min(ans, dist(K, kp) + dist(kp, id[N - 1]) + dist(id[1], id[N - 1]));
      ans =
          min(ans, dist(K, id[N - 1]) + dist(id[N - 1], id[1]) + dist(id[1], kp));
      ans =
          min(ans, dist(K, id[1]) + dist(id[N - 1], id[1]) + dist(id[N - 1], kp));
      for (int nk = 2; nk <= nid; ++nk)
        ans = min(ans, dist(K, id[N - 1]) + dist(id[N - 1], id[nk]) +
                           dist(id[nk], kp) + dist(kp, id[1]) +
                           dist(id[1], id[nk - 1]));
      for (int nk = nid; nk < N - 1; ++nk)
        ans = min(ans, dist(K, id[1]) + dist(id[1], id[nk]) + dist(id[nk], kp) +
                           dist(kp, id[N - 1]) + dist(id[N - 1], id[nk + 1]));
      for (int nk = 2; nk <= nid; ++nk)
        ans = min(ans, dist(K, id[N - 1]) + dist(id[N - 1], id[nk]) +
                           dist(id[nk], kp) + dist(kp, id[nk - 1]) +
                           dist(id[1], id[nk - 1]));
      for (int nk = nid; nk < N - 1; ++nk)
        ans = min(ans, dist(K, id[1]) + dist(id[1], id[nk]) + dist(id[nk], kp) +
                           dist(kp, id[nk + 1]) + dist(id[N - 1], id[nk + 1]));
    }
    printf("%.10lf\n", ans);
    return 0;
  }
  