题目链接:http://openoj.awaysoft.com:8080/judge/contest/view.action?cid=384#problem/D
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
#include <stdio.h>
#include < string.h>
#include <math.h>
double f[ 110][ 110];
int vist[ 110];
struct node
{
double x,y,z,r;
}p[ 110];
double ff(node a,node b)
{
return (sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))-a.r-b.r);
}
int main()
{
int n,i,j;
while (~scanf( " %d ",&n) && n)
{
for (i= 1;i<=n;i++)
scanf( " %lf%lf%lf%lf ",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
for (i= 1;i<n;i++)
for (j=i+ 1;j<=n;j++)
{
double l=ff(p[i],p[j]);
if (l> 0) f[i][j]=f[j][i]=l;
else f[i][j]=f[j][i]= 0.000;
}
memset(vist, 0, sizeof(vist));
vist[ 1]= 1;
int pos,t=n- 1;
double s= 0;
while (t--)
{
double min= 110.000;
for (i= 2;i<=n;i++)
if (!vist[i] && f[ 1][i]<min)
{
pos=i;
min=f[ 1][i];
}
if (min- 110.000== 0) break;
s+=min;
vist[pos]= 1;
for (i= 2;i<=n;i++)
if (!vist[i] && f[pos][i]<f[ 1][i])
f[ 1][i]=f[pos][i];
}
printf( " %0.3f\n ",s);
}
return 0;
}
#include < string.h>
#include <math.h>
double f[ 110][ 110];
int vist[ 110];
struct node
{
double x,y,z,r;
}p[ 110];
double ff(node a,node b)
{
return (sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))-a.r-b.r);
}
int main()
{
int n,i,j;
while (~scanf( " %d ",&n) && n)
{
for (i= 1;i<=n;i++)
scanf( " %lf%lf%lf%lf ",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
for (i= 1;i<n;i++)
for (j=i+ 1;j<=n;j++)
{
double l=ff(p[i],p[j]);
if (l> 0) f[i][j]=f[j][i]=l;
else f[i][j]=f[j][i]= 0.000;
}
memset(vist, 0, sizeof(vist));
vist[ 1]= 1;
int pos,t=n- 1;
double s= 0;
while (t--)
{
double min= 110.000;
for (i= 2;i<=n;i++)
if (!vist[i] && f[ 1][i]<min)
{
pos=i;
min=f[ 1][i];
}
if (min- 110.000== 0) break;
s+=min;
vist[pos]= 1;
for (i= 2;i<=n;i++)
if (!vist[i] && f[pos][i]<f[ 1][i])
f[ 1][i]=f[pos][i];
}
printf( " %0.3f\n ",s);
}
return 0;
}