hdu 1162 Eddy's picture
http://acm.hdu.edu.cn/showproblem.php?pid=1162
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 #define maxn 200 7 using namespace std; 8 const int inf=1<<30; 9 10 double g[maxn][maxn]; 11 int n; 12 bool vis[maxn]; 13 double dist[maxn]; 14 double sum; 15 struct node 16 { 17 double x,y; 18 }p[maxn]; 19 20 double sqr(double x) 21 { 22 return x*x; 23 } 24 25 double dis(int x1,int y1,int x2,int y2) 26 { 27 return sqrt(sqr(x1-x2)+sqr(y1-y2)); 28 } 29 30 void prim() 31 { 32 memset(vis,false,sizeof(vis)); 33 for(int i=1; i<=n; i++) dist[i]=g[1][i]; 34 dist[1]=0; 35 vis[1]=true; 36 for(int i=1; i<n; i++) 37 { 38 double m=(double)inf; 39 int x; 40 for(int j=1; j<=n; j++) if(!vis[j]&&dist[j]<m) m=dist[x=j]; 41 sum+=m; 42 vis[x]=true; 43 for(int j=1; j<=n; j++) if(!vis[j]) dist[j]=min(dist[j],g[x][j]); 44 } 45 } 46 47 48 int main() 49 { 50 while(scanf("%d",&n)!=EOF) 51 { 52 for(int i=1; i<=n; i++) 53 { 54 cin>>p[i].x>>p[i].y; 55 } 56 for(int i=1; i<=n; i++) 57 { 58 for(int j=1; j<=n; j++) 59 { 60 double d=dis(p[i].x,p[i].y,p[j].x,p[j].y); 61 g[i][j]=g[j][i]=d; 62 } 63 } 64 sum=0; 65 prim(); 66 printf("%.2lf\n",sum); 67 } 68 return 0; 69 }