最小生成树的题目,prim搞一搞就可以了。
/*
* Author: stormdpzh
* Time: 2012/5/10 11:09:57
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long
using namespace std;
const int M = 105;
const double MaxD = 1000000000.0;
struct Node {
double x, y, z;
double r;
} node[M];
double dis[M][M];
double low[M];
bool visited[M];
int n;
//bool flag;
double getDis(int i, int j)
{
return sqrt((node[i].x - node[j].x) * (node[i].x - node[j].x) +
(node[i].y - node[j].y) * (node[i].y - node[j].y) +
(node[i].z - node[j].z) * (node[i].z - node[j].z));
}
double gao()
{
memset(visited, false, sizeof(visited));
visited[0] = true;
double res = .0;
rep(i, n)
low[i] = dis[0][i];
rep(i, n - 1) {
double tmp = MaxD;
int id;
rep(j, n) {
if(!visited[j] && tmp > low[j]) {
id = j;
tmp = low[j];
}
}
res += tmp;
visited[id] = true;
rep(j, n) {
if(dis[id][j] < low[j])
low[j] = dis[id][j];
}
}
return res;
}
int main()
{
whz(n) {
rep(i, n)
scanf("%lf%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z, &node[i].r);
rep(i, n) rep(j, n) {
dis[i][j] = dis[j][i] = getDis(i, j) - node[i].r - node[j].r;
if(dis[i][j] < .0)
dis[j][i] = dis[i][j] = .0;
}
printf("%.3f\n", gao());
}
return 0;
}