标签:
3 1.0 1.0 2.0 2.0 2.0 4.0
3.41
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> using namespace std; const int maxn=105; double mp[maxn][maxn]; double dis[maxn],x[maxn],y[maxn]; int vis[maxn]; int i,j,n,l,k; double minn,sum; int main() { while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { double d=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); mp[i][j]=d; mp[j][i]=d; } sum=0; vis[1]=1; for(i=1;i<=n;i++) dis[i]=mp[1][i]; for(i=1;i<n;i++) { minn=INT_MAX*1.0; for(j=1;j<=n;j++) if(!vis[j] && minn>dis[j]) { k=j; minn=dis[j]; } vis[k]=1; sum+=minn; for(j=1;j<=n;j++) if (!vis[j] && dis[j]>mp[k][j]) dis[j]=mp[k][j]; } printf("%.2lf\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5723118.html