poj 2031Building a Space Station
http://poj.org/problem?id=2031
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 const int inf=1<<23; 6 #define maxn 110 7 using namespace std; 8 9 struct point 10 { 11 double x,y,z; 12 double r; 13 }p[110]; 14 int n; 15 double g[maxn][maxn]; 16 bool vis[maxn]; 17 double dist[maxn],ans; 18 19 double sqr(double a) 20 { 21 return a*a; 22 } 23 24 inline double Sqrt(double a) 25 { 26 return a<=0?0:sqrt(a); 27 } 28 29 double dis(point a,point b) 30 { 31 return (Sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)+sqr(a.z-b.z))-a.r-b.r); 32 } 33 34 bool prim() 35 { 36 memset(vis,false,sizeof(vis)); 37 for(int i=0; i<n; i++) 38 dist[i]=inf; 39 ans=0;dist[1]=0; 40 for(int i=1; i<=n; i++) 41 { 42 double temp=inf; 43 int k=0; 44 for(int j=0; j<n; j++) 45 { 46 if(!vis[j]&&dist[j]<temp) 47 { 48 temp=dist[j]; 49 k=j; 50 } 51 } 52 if(temp==inf) return false; 53 vis[k]=true; 54 ans+=temp; 55 for(int j=0; j<n; j++) 56 { 57 if(!vis[j]&&dist[j]>g[k][j]) 58 dist[j]=g[k][j]; 59 } 60 } 61 return false; 62 } 63 64 int main() 65 { 66 while(scanf("%d",&n)&&n){ 67 memset(g,inf,sizeof(g)); 68 for(int i=0; i<n; i++) 69 { 70 scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r); 71 } 72 for(int i=0; i<n; i++) 73 { 74 for(int j=0; j<n; j++) 75 { 76 if(dis(p[i],p[j])<=0) 77 { 78 g[i][j]=0; 79 } 80 else g[i][j]=dis(p[i],p[j]); 81 } 82 } 83 prim(); 84 printf("%.3lf\n",ans); 85 } 86 return 0; 87 }