裸prim
//P1265 公路修建 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=987654321; struct cc{ ll x,y; }a[5005]; bool v[5005]; double d[5005]; inline int read(){ int k=0,f=1; char c=getchar(); while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); } while(isdigit(c)){ k=(k<<1)+(k<<3)+(c^48); c=getchar(); } return k*f; } inline double cal(int x,int y){ return sqrt((double)(a[x].x-a[y].x)*(a[x].x-a[y].x)+(double)(a[x].y-a[y].y)*(a[x].y-a[y].y)); } int main(){ int n; n=read(); for(int i=1;i<=n;i++){ a[i].x=read(); a[i].y=read(); d[i]=inf; } int pos; d[1]=0; double ans=0; for(int i=1;i<=n;i++){ double minn=inf; for(int j=1;j<=n;j++){ if(!v[j]&&d[j]<minn){ minn=d[j]; pos=j; } } ans+=minn;v[pos]=1; for(int j=1;j<=n;j++){ double dd=cal(pos,j); //边计算边修改 if(d[j]>dd) d[j]=dd; } } printf("%.2lf",ans); return 0; }