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 = 1e5 + 10;
  const long double inf = 1e18 + 42;
  struct pt {
    long long x, y;
    bool operator<(const pt& o) const { return tie(x, y) < tie(o.x, o.y); }
    bool operator==(const pt& o) const { return tie(x, y) == tie(o.x, o.y); }
    long long dist2(const pt& o) {
      return (x - o.x) * (x - o.x) + (y - o.y) * (y - o.y);
    }
    long double dist(const pt& o) { return sqrt(dist2(o)); }
  };
  bool on_line(pt& p1, pt& p2, pt& p3);
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector<pt> pts(n);
    for (auto& p : pts) {
      cin >> p.x >> p.y;
    }
    pt start = pts[k - 1];
    sort(pts.begin(), pts.end());
    int odd_id = 0;
    if (pts.size() > 3) {
      for (int i = 0; i < n; ++i) {
        vector<int> other;
        for (int j = 0; j < n and other.size() < 3; ++j) {
          if (i != j) {
            other.push_back(j);
          }
        }
        bool ok = false;
        ok |= on_line(pts[i], pts[other[0]], pts[other[1]]);
        ok |= on_line(pts[i], pts[other[0]], pts[other[2]]);
        ok |= on_line(pts[i], pts[other[1]], pts[other[2]]);
        if (!ok) {
          odd_id = i;
          break;
        }
      }
    }
    pt odd = pts[odd_id];
    pts.erase(pts.begin() + odd_id);
    n = pts.size();
    long double tmp = inf;
    for (int i = 0; i < n; ++i) {
      if (pts[i] == start) {
        if (i > 0 and i < n - 1) {
          tmp = min(tmp, start.dist(pts[0]) + pts[0].dist(odd) +
                             odd.dist(pts[i + 1]) + pts[i + 1].dist(pts.back()));
          tmp = min(tmp, start.dist(pts.back()) + pts.back().dist(odd) +
                             odd.dist(pts[i - 1]) + pts[i - 1].dist(pts[0]));
        }
        pts.erase(pts.begin() + i);
        n = pts.size();
        break;
      }
    }
    long double line_length = pts[0].dist(pts.back());
    long double start_odd = start.dist(odd);
    long double start_p1 = start.dist(pts[0]);
    long double start_pn = start.dist(pts.back());
    long double odd_p1 = odd.dist(pts[0]);
    long double odd_pn = odd.dist(pts.back());
    long double to_odd_and_back = inf;
    for (int i = 0; i < n - 1; ++i) {
      to_odd_and_back =
          min(to_odd_and_back,
              pts[i].dist(odd) + odd.dist(pts[i + 1]) - pts[i].dist(pts[i + 1]));
    }
    long double ans =
        min({start_odd + odd_p1 + line_length, start_odd + odd_pn + line_length,
             start_p1 + line_length + odd_pn, start_pn + line_length + odd_p1,
             start_p1 + to_odd_and_back + line_length,
             start_pn + to_odd_and_back + line_length, tmp});
    cout << fixed << setprecision(10) << ans << endl;
  }
  bool on_line(pt& p1, pt& p2, pt& p3) {
    return (p2.x - p1.x) * (p3.y - p2.y) == (p3.x - p2.x) * (p2.y - p1.y);
  }
  