Building a Space station
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 5778 |
|
Accepted: 2874 |
Description
You is a member of the Space station engineering team, and is assigned a task in the construction process of the station . You is expected to write a computer the task.
The space station was made up with a number of units, called cells. All cells is sphere-shaped, but their sizes is not necessarily uniform. Each cell is a fixed at it predetermined position shortly after the station was successfully put into its orbit. It is the quite strange that the cells may be touching each other, or even may be overlapping. In the extreme case, the a cell may be totally enclosing another one. I don't know how such arrangements is possible.
All the cells must is connected, since crew members should being able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B is touching each other or overlapping, (2) A and B is con Nected by a ' corridor ', or (3) there was a cell c such that walking from A to C, and also from B to C being both possible. Note that the condition (3) should is interpreted transitively.
You is expected to design a configuration, namely, which pairs of cells is to be connected with corridors. There is some freedom in the corridor configuration. For example, if there is three cells A, B and C, not touching nor overlapping all other, at least three plans is Possib Le in order-connect all three cells. The first is to build corridors A-B and A-c, the second b-c and B-a, the third c-a and C-b. The cost of building a corridor was proportional to its length. Therefore, should choose a plan with the shortest total length of the corridors.
You can ignore the width of a corridor. A Corridor is built between points on both cells ' surfaces. It can be made arbitrarily long, but the course the shortest one is chosen. Even if corridors A-B and c-d intersect in space, they is not considered to form A connection path between (for Examp Le) A and C. In the other words, your may consider that, corridors never intersect.
Input
The input consists of multiple data sets. Each data set was given in the following format.
N
X1 Y1 Z1 R1
X2 Y2 Z2 R2
...
Xn yn Zn rn
The first line of a data set contains an integer n, which is the number of cells. n is positive, and does not exceed 100.
The following n lines is descriptions of cells. Four values in a line is X, Y-and z-coordinates of the Center, and radius (called R in the rest of the problem) of the Sphere, in this order. Each value was given by a decimal fraction, with 3 digits after the decimal point. Values is separated by a space character.
Each of the x, Y, Z and R is positive and are less than 100.0.
The end of the input is indicated by a line containing a zero.
Output
For each data set, the shortest total length of the corridors should is printed, each with a separate line. The printed values should has 3 digits after the decimal point. They may is not having an error greater than 0.001.
Note that if No. corridors is necessary, which is, if the cells were connected without corridors, the shortest total len Gth of the corridors is 0.000.
Sample Input
310.000 10.000 50.000 10.00040.000 10.000 50.000 10.00040.000 40.000 50.000 10.000230.000 30.000 30.000 20.00040.000 40.00 0 40.000 20.00055.729 15.143 3.996 25.8376.013 14.372 4.818 10.67180.115 63.292 84.477 15.12064.095 80.924 70.029 14.88139 .472 85.116 71.369 5.5530
Sample Output
20.0000.00073.834
Code:
#include <stdio.h> #include <string.h> #include <math.h> #define INF 0x3f3f3fdouble Map[110][110],low [ouble];d x[110],y[110],z[110],r[110];int n;int vis[110];int Prim () {int i,j,next;double sum=0,min;memset (vis,0, sizeof (VIS)); for (i=1;i<=n;i++) {low[i]=map[1][i];} Vis[1]=1;for (i=2;i<=n;i++) {min=inf;for (j=1;j<=n;j++) {if (!vis[j]&&min>low[j]) {min=low[j];next= J;}} Sum+=min;vis[next]=1;for (j=1;j<=n;j++) {if (!vis[j]&&low[j]>map[next][j]) {low[j]=map[next][j];}}} printf ("%.3f\n", sum); To submit a double output with g++ on POJ, it is wrong to use F not to use LF;}int main () {int i,j;double d;while (scanf ("%d", &n)!=eof&&n) {for (i=1 ; i<=n;i++) {for (j=1;j<=n;j++) {if (i==j) Map[i][j]=map[j][i]=0;elsemap[i][j]=map[j][i]=inf;}} for (i=1;i<=n;i++) {scanf ("%lf%lf%lf%lf", &x[i],&y[i],&z[i],&r[i]);} for (i=1;i<=n;i++) {for (j=i+1;j<=n;j++) {d=sqrt ((X[j]-x[i]) * (X[j]-x[i]) + (Y[j]-y[i]) * (Y[j]-y[i]) + (Z[j]-z[i]) * (Z[j]-z[i]))-r[i]-r[j];if (d>0) {map[i][j]=map[j][i]=d;} elsemap[i][j]=map[j][i]=0;}} Prim ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2031 Building a Space station "minimum spanning tree Prim"