UnionFind() { memset(seq, 0, sizeof seq); } intFind(int x){ return !seq[x] ? x : (seq[x] = Find(seq[x])); } boolUnion(int x, int y){ x = Find(x); y = Find(y); if (x == y) returnfalse; seq[x] = y; returntrue; } } U;
int n, k, cnt;
doubleGetPath(Node x, Node y){ double ret = 0; int X = std::abs(x.x - y.x); int Y = std::abs(x.y - y.y); ret = sqrt(X * X + Y * Y); // 勾股定理,初中数学 return ret; }
voidSolve(){ std::sort(edge + 1, edge + 1 + cnt); int tot = 0; for (int i = 1; i <= cnt; ++i) { bool routput = false; if (tot == n - k) routput = true; // 这里用了一个小技巧,加到 (n - k + 1) 条边的时候就可以输出, // 而不用到最后删边,因为边权是经过排序的 if (U.Union(edge[i].prev, edge[i].next)) { ++tot; if (routput) { cout << std::fixed << std::setprecision(2) << edge[i].weight << endl; return; } } } }
intmain(){ IMPROVE_IO(); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> node[i].x >> node[i].y; } // 构造完全图 for (int i = 1; i <= n; ++i) { for (int j = 1; j < i; ++j) { edge[++cnt].prev = i; edge[cnt].next = j; edge[cnt].weight = GetPath(node[i], node[j]); } } Solve(); return0; }