[USACO07DEC]Building Roads S - 洛谷https://www.luogu.com.cn/problem/P2872
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
int n, m;
struct ver {
double x, y;
};
ver vr[MAXN];
struct node {
int u, v;
double val;
};
node edge[MAXN];
int cnt;
int fa[MAXN];
inline void init() {
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
}
int find(int x) {
if (x == fa[x]) {
return x;
}
return fa[x] = find(fa[x]);
}
inline void unite(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
inline bool same(int x, int y) {
return find(x) == find(y);
}
bool cmp(node a, node b) {
return a.val < b.val;
}
double ans;
inline void kruskal() {
int now = 0;
for (int i = 1; i <= cnt; i++) {
if (!same(edge[i].u, edge[i].v)) {
unite(edge[i].u, edge[i].v);
ans += edge[i].val;
// printf("%d %d %.2lf\n", edge[i].u, edge[i].v, edge[i].val);
now++;
}
if (now == n - 1) {
printf("%.2lf", ans);
return;
}
}
}
int main() {
scanf("%d %d", &n, &m);
init();
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &vr[i].x, &vr[i].y);
}
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
double t = sqrt((vr[i].x - vr[j].x) * (vr[i].x - vr[j].x) + (vr[i].y - vr[j].y) * (vr[i].y - vr[j].y));
edge[++cnt] = node{i, j, t};
// printf("%d======%d %.2lf\n", edge[cnt].u, edge[cnt].v, edge[cnt].val);
}
}
int u, v;
for (int i = 1; i <= m; i++) {
scanf("%d %d", &u, &v);
edge[++cnt] = node{u, v, 0};
}
sort(edge + 1, edge + cnt + 1, cmp);
kruskal();
return 0;
}