Problem p02764 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

using Vect = pair<long double, long double>;

Vect operator+(Vect lhs, Vect rhs) {
  Vect ret = make_pair(0, 0);
  ret.first = lhs.first + rhs.first;
  ret.second = lhs.second + rhs.second;
  return ret;
}
Vect operator-(Vect &lhs, Vect &rhs) {
  Vect ret;
  ret.first = lhs.first - rhs.first;
  ret.second = lhs.second - rhs.second;
  return ret;
}

Vect operator*(long double lhs, const Vect &rhs) {
  return make_pair(lhs * rhs.first, lhs * rhs.second);
}

long double norm(const Vect &v) {
  return sqrt(v.first * v.first + v.second * v.second);
}

Vect rotate(Vect v, long double cost, long double sint) {
  return make_pair(v.first * cost - v.second * sint,
                   v.first * sint + v.second * cost);
}

const long double EPS = 1e-12;

int main() {
  int N, K;
  cin >> N >> K;

  vector<pair<Vect, long double>> xyc(N);
  for (int i = 0; i < N; i++) {
    long double x, y, c;
    cin >> x >> y >> c;
    xyc[i] = make_pair(make_pair(x, y), c);
  }

  long double ur = 1e7, lr = 0;
  for (int _ = 0; _ < 100; _++) {
    // cout << lr << ' ' << ur << endl;
    long double mid = (lr + ur) / 2;
    vector<Vect> center_cands;

    for (auto v : xyc) {
      center_cands.push_back(v.first);
    }

    for (auto m1 : xyc) {
      for (auto m2 : xyc) {
        long double r1 = mid / m1.second, r2 = mid / m2.second;
        long double l = norm(m1.first - m2.first);
        if (l < EPS) {
          continue;
        }
        if ((l <= r1 + r2 + EPS) && (l >= abs(r1 - r2)) - EPS) {
          long double cost = (r1 * r1 + l * l - r2 * r2) / (2 * r1 * l);
          long double sint = sqrt(1 - cost * cost);
          // cout << cost << ' ' << sint << endl;
          Vect diff = m2.first - m1.first;
          //          Vect diff_norm = (1 / l) * (m2.first - m1.first);
          Vect v1 = m1.first + (r1 / l) * rotate(diff, cost, sint);
          Vect v2 = m1.first + (r1 / l) * rotate(diff, cost, -sint);
          center_cands.push_back(v1);
          center_cands.push_back(v2);
        }
      }
    }
    /*
    for(auto c: center_cands){
    cout << '(' << c.first << ',' << c.second << ')' << ' ';
    }cout << endl;
    */

    int max_cnt = 0;
    for (auto center : center_cands) {
      int cnt = 0;
      for (auto p : xyc) {
        long double dist = norm(center - p.first) * p.second;
        if (dist <= mid + EPS) {
          cnt++;
        }
      }
      max_cnt = max(max_cnt, cnt);
    }
    // cout << max_cnt << endl;
    if (max_cnt >= K) {
      ur = mid;
    } else {
      lr = mid;
    }
  }
  cout << setprecision(20) << (lr + ur) / 2 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

using Vect = pair<long double, long double>;

Vect operator+(Vect lhs, Vect rhs) {
  Vect ret = make_pair(0, 0);
  ret.first = lhs.first + rhs.first;
  ret.second = lhs.second + rhs.second;
  return ret;
}
Vect operator-(Vect &lhs, Vect &rhs) {
  Vect ret;
  ret.first = lhs.first - rhs.first;
  ret.second = lhs.second - rhs.second;
  return ret;
}

Vect operator*(long double lhs, const Vect &rhs) {
  return make_pair(lhs * rhs.first, lhs * rhs.second);
}

long double norm(const Vect &v) {
  return sqrt(v.first * v.first + v.second * v.second);
}

Vect rotate(Vect v, long double cost, long double sint) {
  return make_pair(v.first * cost - v.second * sint,
                   v.first * sint + v.second * cost);
}

const long double EPS = 1e-12;

int main() {
  int N, K;
  cin >> N >> K;

  vector<pair<Vect, long double>> xyc(N);
  for (int i = 0; i < N; i++) {
    long double x, y, c;
    cin >> x >> y >> c;
    xyc[i] = make_pair(make_pair(x, y), c);
  }

  long double ur = 1e7, lr = 0;
  for (int _ = 0; _ < 100; _++) {
    // cout << lr << ' ' << ur << endl;
    long double mid = (lr + ur) / 2;
    vector<Vect> center_cands;

    for (auto v : xyc) {
      center_cands.push_back(v.first);
    }

    for (auto m1 : xyc) {
      for (auto m2 : xyc) {
        long double r1 = mid / m1.second, r2 = mid / m2.second;
        long double l = norm(m1.first - m2.first);
        if (l < EPS) {
          continue;
        }
        if ((l <= r1 + r2) && (l >= abs(r1 - r2))) {
          long double cost = (r1 * r1 + l * l - r2 * r2) / (2 * r1 * l);
          long double sint = sqrt(1 - cost * cost);
          // cout << cost << ' ' << sint << endl;
          Vect diff = m2.first - m1.first;
          //          Vect diff_norm = (1 / l) * (m2.first - m1.first);
          Vect v1 = m1.first + (r1 / l) * rotate(diff, cost, sint);
          Vect v2 = m1.first + (r1 / l) * rotate(diff, cost, -sint);
          center_cands.push_back(v1);
          center_cands.push_back(v2);
        }
      }
    }
    /*
    for(auto c: center_cands){
    cout << '(' << c.first << ',' << c.second << ')' << ' ';
    }cout << endl;
    */

    int max_cnt = 0;
    for (auto center : center_cands) {
      int cnt = 0;
      for (auto p : xyc) {
        long double dist = norm(center - p.first) * p.second;
        if (dist <= mid + EPS) {
          cnt++;
        }
      }
      max_cnt = max(max_cnt, cnt);
    }
    // cout << max_cnt << endl;
    if (max_cnt >= K) {
      ur = mid;
    } else {
      lr = mid;
    }
  }
  cout << setprecision(20) << (lr + ur) / 2 << endl;
}