n是球數量,每行四個數代表坐标xyz和球半徑。球之間建邊,球之間相接觸就不需要建邊,求建邊最小和。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include<algorithm>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define V 200+5
#define LL long long int
#define E 320000
#define pow(a) ((a)*(a))
int n, m;
double cost[V][V];
double minCost[V];
int vis[V];
double sum;
struct node{
double x, y, z, r;
};
node vet[V];
void prim(){
for (int i = ; i <= n; i++){
minCost[i] = cost[][i];
vis[i] = ;
}
while (){
int v = -;
for (int i = ; i <= n; i++){
if (!vis[i] && (v == - || minCost[i] < minCost[v]))v = i;
}
if (v == -)break;
vis[v] = ;
sum += minCost[v];
for (int i = ; i <= n; i++){
if (minCost[i]>cost[v][i])minCost[i] = cost[v][i];
}
}
printf("%.3f\n", sum);
}
int main(){
while (cin >> n)
{
if (n == )break;
sum = ;
mem(cost, );
for (int i = ; i <= n; i++){
cin >> vet[i].x >> vet[i].y >> vet[i].z >> vet[i].r;
for (int j = ; j < i; j++){
double dis = sqrt(pow(vet[i].x - vet[j].x) + pow(vet[i].y - vet[j].y) + pow(vet[i].z - vet[j].z));
dis -= vet[i].r + vet[j].r;
if (dis>)
cost[i][j] = cost[j][i] = dis;
}
}
prim();
}
}