#include #include #include #include using namespace std; struct Pos { double x, y, z; Pos() {} Pos(double xx, double yy, double zz) : x(xx), y(yy), z(zz) {} double dist(const Pos& b) { double dx = x - b.x; double dy = y - b.y; double dz = z - b.z; return sqrt(dx * dx + dy * dy + dz * dz); } }; int n; Pos p[110]; double r[110]; double dist[110][110]; double Solve() { double ans = 0; bool v[110]; memset(v, 0, sizeof(v)); multimap edge; edge.insert(make_pair(0.0, 0)); while(!edge.empty()) { double d = edge.begin()->first; int c = edge.begin()->second; edge.erase(edge.begin()); if(v[c]) continue; v[c] = true; ans += d; for(int i = 0; i < n; ++i) edge.insert(make_pair(dist[c][i], i)); } return ans; } int main() { while(cin >> n && n) { for(int i = 0; i < n; ++i) { double x,y,z; cin >> x >> y >> z >> r[i]; p[i] = Pos(x,y,z); } for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) { double d = p[i].dist(p[j]) - r[i] - r[j]; if(d < 0) d = 0.0; dist[i][j] = d; } double ans = Solve(); printf("%.3f\n", ans); } }