poj--2031 minimum Spanning tree (MST) Kruskal algorithm building a Space station
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 4985 |
|
Accepted: 2503 |
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
Test instructions: Connect each cell to MST with the fewest paths, and for the cells that contact each other by default is connected
Idea: It is obvious that MST, two slots: 1, the last line of the topic pulled the intersection, and finally a sentence without considering the situation of intersection ... 2, will have contacted the cells to join and check the set incredibly WA, directly built edge of 0 Bencai ac ...
#include <iostream>#include<stdio.h>#include<cstring>#include<stdlib.h>#include<algorithm>#include<math.h>using namespacestd;Const intmaxn= -;Const intInf= (1<< -);Const Doubleeps=0.0000001;intN;structcell{Doublex, Y, Z DoubleR;}; Cell CELL[MAXN];structedge{intu,v; DoubleLen; FriendBOOL operator<(Edge A,edge b) {returna.len<B.len; }}; Edge EDGE[MAXN*MAXN];inte;intFA[MAXN];intFindintx) { returnfa[x]==x?x:fa[x]=find (Fa[x]);}voidAdd_edge (intIintj) { DoubleDist=sqrt (Pow (cell[i].x-cell[j].x,2) +pow (CELL[I].Y-CELL[J].Y,2) +pow (Cell[i].z-cell[j].z,2))-cell[i].r-CELL[J].R; if(dist<=0) dist=0; EDGE[E].U=i; EDGE[E].V=J; Edge[e++].len=Dist;}DoubleKruskal () {Doubleans=0; for(intI=0; i<n;i++) fa[i]=i; E=0; for(intI=0; i<n-1; i++){ for(intj=i+1; j<n;j++) {Add_edge (i,j); }} sort (Edge,edge+e); for(intI=0; i<e;i++){ intx=find (EDGE[I].U); inty=find (EDGE[I].V); if(x!=y) {Fa[x]=y; Ans+=Edge[i].len; } } returnans;}intMain () { while(cin>>n,n) { for(intI=0; i<n;i++) Cin>>cell[i].x>>cell[i].y>>cell[i].z>>CELL[I].R; printf ("%.3f\n", Kruskal ()); } return 0;}
View Code
poj--2031 minimum Spanning tree (MST) Kruskal algorithm