POJ

来源:互联网 发布:知乎账号购买 编辑:程序博客网 时间:2024/05/20 07:19

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 = 1; i <= n; i++){        minCost[i] = cost[1][i];        vis[i] = 0;    }    while (1){        int v = -1;        for (int i = 1; i <= n; i++){            if (!vis[i] && (v == -1 || minCost[i] < minCost[v]))v = i;        }        if (v == -1)break;        vis[v] = 1;        sum += minCost[v];        for (int i = 1; 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 == 0)break;        sum = 0;        mem(cost, 0);        for (int i = 1; i <= n; i++){            cin >> vet[i].x >> vet[i].y >> vet[i].z >> vet[i].r;            for (int j = 1; 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>0)                    cost[i][j] = cost[j][i] = dis;            }        }        prim();    }}