标签:
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Input
Output
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int read(){ 9 int x=0,f=1;char ch=getchar(); 10 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 11 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 12 return x*f; 13 } 14 const int mxn=120; 15 int n; 16 struct point{ 17 double x;double y; 18 }p[mxn]; 19 double dist(point a,point b){ 20 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) ); 21 } 22 struct edge{ 23 int x,y; 24 double dis; 25 }e[mxn*mxn];int cnt=0; 26 int cmp(edge a,edge b){ 27 return a.dis<b.dis; 28 } 29 int fa[mxn]; 30 void init(){ 31 for(int i=1;i<=n;i++)fa[i]=i; 32 } 33 int find(int x){ 34 if(fa[x]==x)return x; 35 return fa[x]=find(fa[x]); 36 } 37 double ans=0; 38 void Kruskal(){ 39 int i,j; 40 int ti=0; 41 ans=0; 42 for(i=1;i<=cnt;i++){ 43 int u=find(e[i].x),v=find(e[i].y); 44 if(u==v)continue; 45 ti++; 46 ans+=e[i].dis; 47 fa[u]=v; 48 if(ti==n-1)break; 49 } 50 return; 51 } 52 int main(){ 53 int i,j; 54 while(scanf("%d",&n)!=EOF){ 55 init(); 56 cnt=0; 57 int u,v,dis;int m; 58 int i,j; 59 for(i=1;i<=n;i++){ 60 scanf("%lf%lf",&p[i].x,&p[i].y); 61 } 62 for(i=1;i<n;i++) 63 for(j=i+1;j<=n;j++){ 64 e[++cnt]=(edge){i,j,dist(p[i],p[j])}; 65 } 66 sort(e+1,e+cnt+1,cmp); 67 Kruskal(); 68 printf("%.2f\n",ans); 69 } 70 return 0; 71 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5891016.html