#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
const double eps = 1e-6;
struct edge{
int to,next;
double dis;
}e[maxn];
struct edge1{
int u,v;
double w;
}e1[maxn];
struct node{
int x,y;
}pos[maxn];
int n,m,head[maxn],cnt,a,b,f[maxn];
inline int read(){
int x = 0,f = 1;
char ch = getchar();
while(!isdigit(ch)){if(ch == '-') f = -1;ch = getchar();}
while(isdigit(ch)){x = x * 10 + ch - '0';ch = getchar();}
return f * x;
}
/*inline void add_edge(int u,int v,double d){
e[++cnt].to = v;
e[cnt].dis = d;
e[cnt].next = head[u];
head[u] = cnt;
}*/
inline double getDis(int p,int q){
int tx = abs(pos[p].x - pos[q].x);
int ty = abs(pos[p].y - pos[q].y);
double ans = sqrt(tx * tx + ty * ty);
return ans;
}
inline int find(int x){
if(f[x] != x)
f[x] = find(f[x]);
return f[x];
}
inline bool cmp(edge1 p,edge1 q){
return p.w - q.w < eps;
}
int main(){
n = read(),m = read();
for(int i = 1;i <= n;i++){
f[i] = i;
}
for(int i = 1;i <= n;i++){
a = read(),b = read();
pos[i].x = a;
pos[i].y = b;
}
for(int i = 1;i <= m;i++){
a = read(),b = read();
int fa = find(a);
int fb = find(b);
if(fa != fb)
f[fb] = f[fa];
}
int c = 0,ti,tj;
for(int i = 1;i <= n;i++)
for(int j = i + 1;j <= n;j++){
ti = find(i),tj = find(j);
if(ti != tj && i != j){
e1[++cnt].u = i;
e1[cnt].v = j;
e1[cnt].w = getDis(i,j);
// add_edge(i,j,getDis(i,j));
}
}
sort(e1 + 1,e1 + 1 + cnt,cmp);
double sum = 0.00;
for(int i = 1;i <= cnt;i++){
ti = find(e1[i].u);
tj = find(e1[i].v);
if(ti != tj){
f[tj] = f[ti];
sum += e1[i].w;
c++;
}
if(c == n - m - 1){
break;
}
}
printf("%.2f",sum);
return 0;
}
如果把getDis函数中abs去掉就WA了
但是有3个点AC了
请dalao指教