https://vjudge.net/problem/POJ-2031
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<cstdio>
#define mem(s,value) memset(s,value,sizeof(s))
#define frein(s) freopen(s,"r",stdin)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 400;
class Point{
public:
double x,y,z,r;
Point(){};
Point(double x_,double y_,double z_,double r_):x(x_),y(y_),z(z_),r(r_){};
double operator- (const Point& rhs)const{
double s = (x-rhs.x)*(x-rhs.x)+(y-rhs.y)*(y-rhs.y)+(z-rhs.z)*(z-rhs.z);
double d = (r+rhs.r)*(r+rhs.r);
double p = s - d;
return p<=0? 0 : sqrt(s)-sqrt(d);
}
};
Point all[maxn];
double mapper[maxn][maxn];
int n;
int readin(){
scanf("%d",&n);
if(!n)return 0;
//初始化数组
mem(mapper,0);
//开始读入
for(int i=0;i<n;i++){
double x,y,z,r;
cin>>x>>y>>z>>r;
all[i] = Point(x,y,z,r);
for(int k=0;k<i;k++){
//更新不同的点之间的距离
mapper[i][k] = mapper[k][i] = (all[i]-all[k]);
}
mapper[i][i]=0;
}
return 1;
}
double dis[maxn];
int vis[maxn];
double Value;
int solve(){
Value = 0;
mem(vis,0);
vis[0]=1;
for(int i=0;i<n;i++)dis[i]=mapper[0][i];
//
for(int i=1;i<n;i++){
double minc=0x3f3f3f3f;
int p = -1;
for(int k=0;k<n;k++){
if(!vis[k] && minc>dis[k]){
minc = dis[k];
p = k;
}
}
Value += minc;
vis[p] = 1;
for(int k=0;k<n;k++){
dis[k] = min(dis[k],mapper[p][k]);
}
}
printf("%.3lf
",Value);
return 0;
}
int main(){
//frein("in.txt");
while(readin()){
solve();
//break;
}
return 0;
}
//https://vjudge.net/problem/POJ-2031
OK