标签:
链接:
http://poj.org/problem?id=2031
代码:
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 110; const int INF = 0xfffffff; struct node { double x, y, z, r; }s[N]; int n; double J[N][N], dist[N]; bool vis[N]; double Prim() { int i, j, index; double ans=0; memset(vis, 0, sizeof(vis)); vis[1]=1; for(i=1; i<=n; i++) dist[i]=J[1][i]; for(i=1; i<n; i++) { double MIN=INF; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]<MIN) { index=j; MIN=dist[j]; } } vis[index]=1; if(MIN<INF) ans += MIN; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]>J[index][j] ) dist[j]=J[index][j]; } } return ans; } int main () { while(scanf("%d", &n),n) { int i, j; memset(s, 0, sizeof(s)); memset(J, 0, sizeof(J)); for(i=1; i<=n; i++) scanf("%lf%lf%lf%lf", &s[i].x, &s[i].y, &s[i].z, &s[i].r); for(i=1; i<=n; i++) for(j=1; j<i; j++) { double k=sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y)+(s[i].z-s[j].z)*(s[i].z-s[j].z))-s[i].r-s[j].r; if(k<0) J[i][j]=J[j][i]=0; else J[i][j]=J[j][i]=k; } double ans=Prim(); printf("%.3f\n", ans); } return 0; }
(最小生成树) Building a Space Station -- POJ -- 2031
标签:
原文地址:http://www.cnblogs.com/YY56/p/4735028.html