标签:out 长度 inline ems head its 输出 struct 问题
4 1
1 1
3 1
2 3
4 3
1 4
4.00
#include<bits/stdc++.h> #include <math.h> using namespace std; typedef long long ll; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } const int maxn=5e5+10; struct node{ int x,y; double z; }a[maxn]; double ans; int n,m,len,p; int x[maxn],y[maxn]; int fa[maxn]; bool cmp(node x,node y){ return x.z<y.z; } double di(int i,int j){ double xx,yy; xx=(double)(x[i]-x[j]); yy=(double)(y[i]-y[j]); return sqrt(xx*xx+yy*yy); } int find(int h) { return fa[h]==h?h:fa[h]=find(fa[h]); } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ scanf("%d",&x[i]); scanf("%d",&y[i]); } for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++){ a[++len].x=i; a[len].y=j; a[len].z=di(i,j); } } sort(a+1,a+len+1,cmp); for(int i=1;i<=n;i++){ fa[i]=i; } p=n; int s,t; for(int i=1;i<=m;i++){ s=read(); t=read(); int diss=find(s); int dist=find(t); if(diss!=dist){ p--; fa[dist]=diss; } } for(int i=1;i<=len;i++){ if(p==1){ break; } int s=a[i].x; int t=a[i].y; int diss=find(s); int dist=find(t); if(diss!=dist){ p--; fa[dist]=diss; ans+=a[i].z; } } printf("%.2lf",ans); }
标签:out 长度 inline ems head its 输出 struct 问题
原文地址:https://www.cnblogs.com/lipu123/p/13284957.html