main.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. const int S = 100;
  7. const int P = 500;
  8. struct V {
  9. int x;
  10. int y;
  11. V(int x = 0, int y = 0) : x(x), y(y) {}
  12. double operator-(const V &that) const {
  13. double dx = this->x - that.x;
  14. double dy = this->y - that.y;
  15. return sqrt(dx * dx + dy * dy);
  16. }
  17. } outpost[P];
  18. struct E {
  19. int u;
  20. int v;
  21. double len;
  22. E(int u, int v, double len) : u(u), v(v), len(len) {}
  23. bool operator<(const E &that) const { return this->len > that.len; }
  24. };
  25. struct UF {
  26. int id[P];
  27. int sz[P];
  28. int cnt;
  29. } uf;
  30. int find(int i) {
  31. if (i == uf.id[i]) return i;
  32. int j = find(uf.id[i]);
  33. uf.id[i] = j;
  34. return j;
  35. }
  36. void unite(int i, int j) {
  37. i = find(i);
  38. j = find(j);
  39. if (i == j) return;
  40. if (uf.sz[i] < uf.sz[j]) {
  41. uf.id[i] = j;
  42. } else {
  43. if (uf.sz[i] == uf.sz[j]) uf.sz[i]++;
  44. uf.id[j] = i;
  45. }
  46. uf.cnt--;
  47. }
  48. int main() {
  49. int n, s, p, k;
  50. scanf("%d", &n);
  51. while (n--) {
  52. scanf("%d %d", &s, &p);
  53. for (int i = 0; i < p; i++) {
  54. scanf("%d %d", &outpost[i].x, &outpost[i].y);
  55. uf.id[i] = i;
  56. }
  57. memset(uf.sz, 0, sizeof(uf.sz));
  58. uf.cnt = p, k = 0;
  59. priority_queue<E> pq;
  60. for (int i = 0; i < p - 1; i++)
  61. for (int j = i + 1; j < p; j++) pq.push(E(i, j, outpost[i] - outpost[j]));
  62. double d = 0.;
  63. while (1 < uf.cnt) {
  64. E e = pq.top(); // No const E & !!! WA * 4
  65. pq.pop();
  66. if (find(e.u) == find(e.v)) continue;
  67. unite(e.u, e.v);
  68. if (++k == p - s) {
  69. d = e.len;
  70. break;
  71. }
  72. }
  73. printf("%.2f\n", d);
  74. }
  75. return 0;
  76. }