标签:
题目链接:http://poj.org/problem?id=2031
n个球型的cell,如果任意两个球表面没有接触或者没有包含关系,就选择最近的表面建立通道;
所以用maps[i][j]表示i,j之间的距离;
注意:
poj上:对于双精度输出,G++上面要用%f,C++则用%lf,否则WA
#include<stdio.h> #include<string.h> #include<map> #include<iostream> #include<algorithm> #include<math.h> #define N 110 #define INF 0xfffffff using namespace std; int vis[N], n; double maps[N][N], dist[N]; struct node { double x, y, z, r; }a[N*N]; void Init() { int i; memset(a, 0,sizeof(a)); memset(vis, 0 ,sizeof(vis)); for(i=0; i<=n; i++) { dist[i] = INF; for(int j=0; j<=n; j++) { maps[i][j] = INF; } maps[i][i] = 0; } } double Prim(int start) { double ans = 0; vis[start] = 1; for(int i=1; i<=n; i++) dist[i] = maps[start][i]; for(int i=1; i<=n; i++) { double Min = INF; int index = -1; for(int j=1; j<=n; j++) { if(vis[j] ==0 && Min > dist[j]) { Min = dist[j]; index = j; } } if(index == -1)break; vis[index] = 1; ans += Min; for(int j=1; j<=n; j++) { if(vis[j] == 0 && dist[j] > maps[index][j]) dist[j] = maps[index][j]; } } return ans; } int main() { int i, j; double ans, x, y, z, d; while(scanf("%d", &n), n) { Init(); for(i=1; i<=n; i++) { scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r); } for(i=1; i<=n; i++) { for(j=i+1; j<=n; j++) { x = a[i].x - a[j].x; y = a[i].y - a[j].y; z = a[i].z - a[j].z; d = sqrt( x*x + y*y + z*z ); if(d > a[i].r + a[j].r) { maps[i][j] = maps[j][i] = d - a[i].r - a[j].r; } else { maps[i][j] = maps[j][i] = 0; } } } ans = Prim(1); printf("%.3lf\n", ans); } return 0; }
Building a Space Station---poj2031
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/4680735.html