Problem p02764 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define write(x) cout << (x) << '\n'
using namespace std;
typedef long long ll;
template <class T> using vv = vector<vector<T>>;
template <class T> auto vvec(int n, int m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
constexpr int INF = 1 << 29, MOD = int(1e9) + 7;
constexpr ll LINF = 1LL << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  };
} aaaa;

constexpr double EPS = 1e-3;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> X(N), Y(N), C(N);
  rep(i, N) cin >> X[i] >> Y[i] >> C[i];

  if (K == 1) {
    write(0);
    return 0;
  }

  double low = 0, high = 3e5;
  rep(it, 60) {
    double mid = (low + high) / 2;
    // mid = 3;
    bool ok = false;
    rep(i, N - 1) rep(j, i + 1, N) {
      double d = hypot(X[i] - X[j], Y[i] - Y[j]);
      if (d * (C[i] * C[j]) > mid * (C[j] + C[i])) {
        continue;
      }
      rep(p, 2) {
        double x, y;
        if (d * (C[i] * C[j]) < mid * abs(C[j] - C[i])) {
          if (C[i] < C[j])
            x = X[j], y = Y[j];
          else
            x = X[i], y = Y[i];
        } else {
          double d2 = pow(d, 2);
          double a = (d2 + pow(mid / C[i], 2) - pow(mid / C[j], 2)) / 2;
          double b = d2 * pow(mid / C[i], 2) - a * a + EPS;
          // if (b < 0) cerr << b << endl;
          assert(b >= 0);
          x = (a * (X[j] - X[i]) + (-2 * p + 1) * (Y[j] - Y[i]) * sqrt(b)) /
                  d2 +
              X[i];
          y = (a * (Y[j] - Y[i]) + (+2 * p - 1) * (X[j] - X[i]) * sqrt(b)) /
                  d2 +
              Y[i];
        }

        int cnt = 0;
        rep(k, N) {
          if (k == i || k == j)
            cnt++;
          else if (hypot(x - X[k], y - Y[k]) * C[k] <= mid)
            cnt++;
        }
        if (cnt >= K)
          ok = true;
      }
      if (ok)
        break;
    }

    if (ok)
      high = mid;
    else
      low = mid;
  }
  write(high);
}

Fixed submission:
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define write(x) cout << (x) << '\n'
using namespace std;
typedef long long ll;
template <class T> using vv = vector<vector<T>>;
template <class T> auto vvec(int n, int m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
constexpr int INF = 1 << 29, MOD = int(1e9) + 7;
constexpr ll LINF = 1LL << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  };
} aaaa;

constexpr double EPS = 1e-3;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> X(N), Y(N), C(N);
  rep(i, N) cin >> X[i] >> Y[i] >> C[i];

  if (K == 1) {
    write(0);
    return 0;
  }

  double low = 0, high = 3e5;
  rep(it, 60) {
    double mid = (low + high) / 2;
    // mid = 3;
    bool ok = false;
    rep(i, N - 1) rep(j, i + 1, N) {
      double d = hypot(X[i] - X[j], Y[i] - Y[j]);
      if (d * (C[i] * C[j]) > mid * (C[j] + C[i])) {
        continue;
      }
      rep(p, 2) {
        double x, y;
        if (d * (C[i] * C[j]) < mid * abs(C[j] - C[i])) {
          if (C[i] < C[j])
            x = X[j], y = Y[j];
          else
            x = X[i], y = Y[i];
        } else {
          double d2 = pow(d, 2);
          double a = (d2 + pow(mid / C[i], 2) - pow(mid / C[j], 2)) / 2;
          double b = d2 * pow(mid / C[i], 2) - a * a + EPS;
          // if (b < 0) cerr << b << endl;
          // assert(b >= 0);
          x = (a * (X[j] - X[i]) + (-2 * p + 1) * (Y[j] - Y[i]) * sqrt(b)) /
                  d2 +
              X[i];
          y = (a * (Y[j] - Y[i]) + (+2 * p - 1) * (X[j] - X[i]) * sqrt(b)) /
                  d2 +
              Y[i];
        }

        int cnt = 0;
        rep(k, N) {
          if (k == i || k == j)
            cnt++;
          else if (hypot(x - X[k], y - Y[k]) * C[k] <= mid)
            cnt++;
        }
        if (cnt >= K)
          ok = true;
      }
      if (ok)
        break;
    }

    if (ok)
      high = mid;
    else
      low = mid;
  }
  write(high);
}