\[O(Mlog\frac{N(N-1)}{2})\approx{O(MlogN^2)}
\]
\[O((N+M)log\frac{N(N-1)}{2})\approx{O((N+M)logN^2)}
\]
\[O((M+\frac{N(N-1)}{2})log\frac{N(N-1)}{2})\approx{O((M+N^2)logN^2)}
\]
\[O(M+\frac{N(N-1)}{2}log\frac{N(N-1)}{2})\approx{O(M+N^2logN^2)}
\]
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define maxn 1001
#define maxm 1000001
using namespace std;
struct edge{
int u,v; double w;
edge(){}
edge(const int &_u,const int &_v,const double &_w){ u=_u,v=_v,w=_w; }
bool operator<(const edge &e)const{ return e.w-w>1e-9; }
}e[maxm];
int fa[maxn];
int x[maxn],y[maxn];
int n,m,k;
double ans;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int get(int x){
if(fa[x]==x) return x;
return fa[x]=get(fa[x]);
}
int main(){
n=read(),m=read();
for(register int i=1;i<=n;i++) x[i]=read(),y[i]=read();
for(register int i=1;i<n;i++){
for(register int j=i+1;j<=n;j++)
e[++k]=edge(i,j,(double)sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j])));
}
sort(e+1,e+1+k);
for(register int i=1;i<=n;i++) fa[i]=i;
for(register int i=1;i<=m;i++){
int u=read(),v=read();
fa[get(u)]=get(v);
}
for(register int i=1;i<=k;i++){
int u=get(e[i].u),v=get(e[i].v);
if(u==v) continue;
fa[u]=v,ans+=e[i].w;
}
printf("%.2lf\n",ans);
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章