View Code
1 #include<iostream> 2 #include<cstdio> 3 #include<math.h> 4 #include<cstring> 5 #define INF 0x3f3f3f 6 struct node 7 { 8 double x,y,z,r; 9 }ht[105]; 10 double h[105][105]; 11 void prim(int n) 12 { 13 int vis[105]; 14 int pos,i,j; 15 double min; 16 double tr[105]; 17 memset(vis,0,sizeof(vis)); 18 for(i=1;i<n;i++) 19 { 20 tr[i]=h[0][i]; 21 vis[0]=1; 22 } 23 double ans=0; 24 for(i=0;i<n-1;i++) 25 { 26 min=INF; 27 for(j=1;j<n;j++) 28 { 29 if(!vis[j]&&tr[j]<min) 30 { 31 min=tr[j]; 32 pos=j; 33 } 34 } 35 vis[pos]=1; 36 ans+=min; 37 for(j=1;j<n;j++) 38 { 39 if(tr[j]>h[pos][j]) 40 tr[j]=h[pos][j]; 41 } 42 } 43 printf("%.3f\n",ans); 44 } 45 double dis(double x1,double y1,double z1,double r1,double x2,double y2,double z2,double r2) 46 { 47 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))-r1-r2; 48 } 49 int main() 50 { 51 int n,i,j; 52 while(~scanf("%d",&n)) 53 { 54 if(n==0) 55 break; 56 for(i=0;i<n;i++) 57 { 58 scanf("%lf%lf%lf%lf",&ht[i].x,&ht[i].y,&ht[i].z,&ht[i].r); 59 } 60 memset(h,0,sizeof(h)); 61 for(i=0;i<n;i++) 62 { 63 for(j=0;j<=i;j++) 64 { 65 h[i][j]=dis(ht[i].x,ht[i].y,ht[i].z,ht[i].r,ht[j].x,ht[j].y,ht[j].z,ht[j].r); 66 if(h[i][j]<0) 67 { 68 h[i][j]=0; 69 70 } 71 h[j][i]=h[i][j]; 72 } 73 } 74 prim(n); 75 } 76 return 0; 77 }