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>
  #pragma GCC optimize(2, 3, "Ofast")
  using namespace std;
  template <typename T1, typename T2>
  void ckmin(T1 &a, T2 b) {
    if (a > b) a = b;
  }
  template <typename T1, typename T2>
  void ckmax(T1 &a, T2 b) {
    if (a < b) a = b;
  }
  int read() {
    int x = 0, f = 0;
    char ch = getchar();
    while (!isdigit(ch)) f |= ch == '-', ch = getchar();
    while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
    return f ? -x : x;
  }
  template <typename T>
  void print(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) print(x / 10);
    putchar(x % 10 + '0');
  }
  template <typename T>
  void print(T x, char let) {
    print(x), putchar(let);
  }
  const int N = 200005;
  struct dot {
    int x, y, id;
  } a[N], b[N];
  int sq[N];
  double ans = 1e9;
  int special, n, m, S, SS;
  dot operator+(dot a, dot b) { return {a.x + b.x, a.y + b.y}; }
  dot operator-(dot a, dot b) { return {a.x - b.x, a.y - b.y}; }
  bool operator==(dot a, dot b) { return 1ll * a.x * b.y == 1ll * a.y * b.x; }
  bool operator!=(dot a, dot b) { return 1ll * a.x * b.y != 1ll * a.y * b.x; }
  inline double sqr(double x) { return x * x; }
  inline double dist(int x, int y) {
    return sqrt(sqr(a[x].x - a[y].x) + sqr(a[x].y - a[y].y));
  }
  int main() {
    scanf("%d%d", &m, &SS);
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &b[i].x, &b[i].y);
      b[i].id = i;
    }
    sort(b + 1, b + m + 1, [&](dot a, dot b) {
      return make_pair(a.x, a.y) <= make_pair(b.x, b.y);
    });
    for (int i = 1; i <= m; i++) {
      if (i == 1 ||
          make_pair(b[i - 1].x, b[i - 1].y) != make_pair(b[i].x, b[i].y))
        a[++n] = b[i];
      if (b[i].id == SS) S = n;
    }
    if (n == 1) {
      puts("0.000000");
      return 0;
    }
    if (n == 2) {
      printf("%.10f\n", dist(1, 2));
      return 0;
    }
    if (n == 3) {
      double ans = 1e9;
      for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
          if (i != S && j != S && i != j) {
            ckmin(ans, dist(S, i) + dist(i, j));
          }
        }
      }
      printf("%.10f\n", ans);
      return 0;
    }
    dot a1 = a[2] - a[1], a2 = a[3] - a[1], a3 = a[3] - a[2], v;
    if (a1 == a2)
      v = a1;
    else if (a1 == a2)
      v = a2;
    else
      v = a3, special = 1;
    if (!special) {
      for (int i = 2; i <= n; i++)
        if ((a[i] - a[1]) != v) special = i;
    }
    if (!special) special = 1;
    for (int i = 1; i <= n; i++) {
      if (i == S) {
        int t = 0;
        double sum = 0;
        if (i == special) {
          for (int j = 1; j <= n; j++) {
            if (j != special) sq[++t] = j;
          }
        } else {
          for (int j = i; j >= 1; j--)
            if (j != special) sq[++t] = j;
          for (int j = i + 1; j <= n; j++)
            if (j != special) sq[++t] = j;
        }
        for (int j = 1; j < n - 1; j++) sum += dist(sq[j], sq[j + 1]);
        if (i != special) {
          ckmin(ans, sum + dist(special, sq[n - 1]));
          for (int j = n - 2; j >= 1; j--)
            ckmin(ans, sum - dist(sq[j], sq[j + 1]) + dist(sq[j], special) +
                           dist(sq[j + 1], special));
        } else {
          ckmin(ans, sum + dist(special, sq[1]));
        }
        t = 0;
        if (i == special) {
          for (int j = n; j >= 1; j--)
            if (j != special) sq[++t] = j;
        } else {
          for (int j = i; j <= n; j++)
            if (j != special) sq[++t] = j;
          for (int j = i - 1; j >= 1; j--)
            if (j != special) sq[++t] = j;
        }
        sum = 0;
        for (int j = 1; j < n - 1; j++) sum += dist(sq[j], sq[j + 1]);
        if (i != special) {
          ckmin(ans, sum + dist(special, sq[n - 1]));
          for (int j = n - 2; j >= 1; j--)
            ckmin(ans, sum - dist(sq[j], sq[j + 1]) + dist(sq[j], special) +
                           dist(sq[j + 1], special));
        } else {
          ckmin(ans, sum + dist(special, sq[1]));
        }
      }
    }
    printf("%.10f\n", ans);
    return 0;
  }
  