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 MAXN = (int)2e5 + 5;
  const double oo = 1e9 + 7;
  struct pt {
    long long x, y;
    pt operator+(pt p) { return {x + p.x, y + p.y}; }
    pt operator-(pt p) { return {x - p.x, y - p.y}; }
    long long operator*(pt p) { return x * p.y - y * p.x; }
    long long operator|(pt p) { return x * p.x + y * p.y; }
    bool inline operator==(const pt &rhs) const {
      return x == rhs.x && y == rhs.y;
    }
  };
  struct line {
    pt v;
    long long c;
    line(pt p, pt q) : v(q - p), c(v * p) {}
    bool is_on(pt p) { return v * p == c; }
  };
  int n, k;
  pt p[MAXN];
  line find_line() {
    for (int i = 0; i < 3; ++i) {
      for (int j = i + 1; j < 3; ++j) {
        line l(p[i], p[j]);
        int cnt = 0;
        for (int t = 0; t < n; ++t)
          if (l.is_on(p[t])) ++cnt;
        if (cnt == n - 1) return l;
      }
    }
    cerr << "Line not found" << endl;
    exit(0);
  }
  int main() {
    cin >> n >> k;
    --k;
    for (int i = 0; i < n; ++i) cin >> p[i].x >> p[i].y;
    pt save = p[k];
    line l = find_line();
    for (int i = 0; i < n; ++i) {
      if (!l.is_on(p[i])) {
        swap(p[i], p[n - 1]);
        break;
      }
    }
    --n;
    auto proj_cmp = [&](pt p, pt q) { return (l.v | p) < (l.v | q); };
    sort(p, p + n, proj_cmp);
    auto dist = [&](int i, int j) {
      return hypot(p[i].x - p[j].x, p[i].y - p[j].y);
    };
    auto cover = [&](int l, int r) {
      return min(dist(l, n), dist(r, n)) + dist(l, r);
    };
    for (int i = 0; i <= n; ++i) {
      if (p[i] == save) {
        k = i;
        break;
      }
    }
    double ans = oo;
    if (k == n)
      ans = cover(0, n - 1);
    else {
      for (int i = 0; i <= k; ++i) {
        double d = min(dist(k, i) + dist(n - 1, n), dist(k, n - 1) + dist(i, n)) +
                   dist(i, n - 1);
        if (i > 0) d += cover(0, i - 1);
        ans = min(ans, d);
      }
      for (int i = k; i < n; ++i) {
        double d =
            min(dist(k, i) + dist(0, n), dist(k, 0) + dist(i, n)) + dist(0, i);
        if (i < n - 1) d += cover(i + 1, n - 1);
        ans = min(ans, d);
      }
    }
    printf("%.15g\n", ans);
    return 0;
  }
  