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 N = 2e5;
  const long double eps = 1e-8;
  bool check(long double x) { return abs(x) < eps; }
  int n, k;
  struct P {
    long double x, y;
    int id;
    P(long double x = 0, long double y = 0, int id = 0) : x(x), y(y), id(id) {}
    long double norm() { return sqrtl(x * x + y * y); }
    P operator-(const P t) const { return P(x - t.x, y - t.y); }
  } p[N + 5], tp, pk;
  int sp;
  long double diss;
  long double dis(P x, P y) { return (x - y).norm(); }
  long double cross(P t1, P t2) { return t1.x * t2.y - t1.y * t2.x; }
  bool chk(P a, P b) {
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (!check(cross(p[i] - a, p[i] - b))) {
        sp = i;
        cnt++;
      }
    return cnt == 1;
  }
  void prep() {
    if (chk(p[1], p[2])) return;
    if (chk(p[1], p[3])) return;
    chk(p[2], p[3]);
  }
  bool cmp(P x, P y) { return check(x.x - y.x) ? x.y < y.y : x.x < y.x; }
  int main() {
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
      scanf("%LF %LF", &p[i].x, &p[i].y);
      p[i].id = i;
    }
    prep();
    tp = p[sp];
    pk = p[k];
    for (int i = sp; i < n; i++) p[i] = p[i + 1];
    n--;
    sort(p + 1, p + n + 1, cmp);
    diss = dis(p[1], p[n]) + min(dis(tp, p[1]), dis(tp, p[n]));
    if (k == sp)
      printf("%.8LF\n", diss);
    else {
      long double d1 = dis(tp, p[1]), dn = dis(tp, p[n]), d1n = dis(p[1], p[n]);
      long double ans = min(dis(tp, pk) + diss,
                            d1n + min(dis(pk, p[n]) + d1, dis(pk, p[1]) + dn));
      for (int i = 1; i < n; i++)
        if (p[i].id == k)
          ans = min(ans,
                    d1n - dis(p[i], p[i + 1]) + d1 + min(dis(tp, p[i + 1]), dn));
      for (int i = 2; i <= n; i++)
        if (p[i].id == k)
          ans = min(ans,
                    d1n - dis(p[i], p[i - 1]) + dn + min(dis(tp, p[i - 1]), d1));
      for (int i = 1; i < n; i++)
        ans =
            min(ans, d1n - dis(p[i], p[i + 1]) +
                         min(dis(pk, p[1]) + dis(p[i], tp), dis(pk, p[i]) + d1) +
                         min(dis(p[i + 1], tp), dn));
      for (int i = 2; i <= n; i++)
        ans =
            min(ans, d1n - dis(p[i - 1], p[i]) +
                         min(dis(pk, p[n]) + dis(p[i], tp), dis(pk, p[i]) + dn) +
                         min(dis(p[i - 1], tp), d1));
      if (p[1].id == k) ans = min(ans, d1n + dis(p[n], tp));
      if (p[n].id == k) ans = min(ans, d1n + dis(p[1], tp));
      printf("%.8LF\n", ans);
    }
    return 0;
  }
  