Link: http://poj.org/problem? Id = 2031 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 22013 # Problem/
Building a space station
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:3578 |
|
Accepted:1830 |
Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with a number of units, called cells. all cells are sphere-shaped, but their sizes are not necessarily uniform. each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. it is Quite strange that two cells may be touching each other, or even may be overlapping. in an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible.All the cells must be connected, since crew members shocould be 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 are touching each other or overlapping, (2) A and B are connected by a 'corridor', or (3) There is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) shocould be interpreted transitively. You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. there is some freedom in the corridor configuration. for example, if there are three cells A, B and C, not touching nor overlapping each other, at least Three plans are possible in order to 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 is proportional to its length. therefore, you shoshould choose a plan The shortest total length of the corridors. You can ignore the width of a corridor. A corridor is built between points on two cells 'surfaces. it can be made arbitrarily long, but of course the shortest one is chosen. even if two corridors A-B and C-D intersect in space, they are not considered to form A connection path between (for example) A and C. In other words, you may consider that two corridors never intersect. Input The input consists of multiple data sets. Each data set is 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 are descriptions of cells. four values in a line are 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 is given by a decimal fraction, with 3 digits after The decimal point. Values are separated by a space character. Each of X, Y, Z and R are positive and is 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 shocould be printed, each in a separate line. the printed values shoshould have 3 digits after the decimal point. they may not have an error greater than 0.001.Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the Shortest total length 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.000 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 Source Japan 2003 Domestic |
[Submit] [Go Back] [Status]
[Discuss]
Question:
In 3D space, give you the coordinates and radius of N sphere
If these sphere are not connected, you need to establish some channels to connect all the sphere.
The connection can be considered when the surface is tangent.
Algorithm:
Minimum Spanning Tree (Kruskal) complexity O (eloge) sorting fast loge can be ignored directly as O (E)
Or
Minimum Spanning Tree prime complexity O (N * n)
Ideas:
Kruskal:
First, create a graph between each sphere, and then sort by the edges from small to large.
Use the check set to check whether two points belong to the same connected component. [This determines whether the two balls on this edge are connected]
If it does not belong to the same Unicom component, connect
Because every time it is the shortest side, the final request must be the shortest distance. Prime: adds an empty connected component from the first vertex,
Find the nearest Vertex on the first vertex,
At this time, there are two points in the component together.
Then, the system keeps searching for a point that is not in the connected component closest to the connected component.
Until the connected component is added to all vertices
Because each time it is the closest point to the [connected component as a whole], the result must be the optimal one. The minimum spanning tree recommended below is also prime, this is what the instructor said in the data structure class.
Related algorithm learning:
Lrj "White Book" P200-P201
Minimum Spanning Tree:
Http://blog.csdn.net/cfreezhan/article/details/8189218
Query set:
Http://blog.csdn.net/cfreezhan/article/details/8629871
Http://blog.csdn.net/cfreezhan/article/category/1219856
Kruskal
/*************************************** * *********** Accepted248 kb32 MSC ++ 1885 B2013-07-26 15:47:09: in 3D space, we will give you the coordinates and radius of N sphere. If these sphere do not communicate with each other, you need to establish some channels to connect all the sphere. The connection can be considered when the surface is tangent. Algorithm: Minimum Spanning Tree (Kruskal) complexity O (e) idea: classical least Spanning Tree questions are first created between the sphere, then sort the edge from small to large and query the set to check whether the two points belong to the same Unicom component. [This determines whether the two balls on this edge are connected.] If the two sides do not belong to the same Unicom component, the connection is the shortest side, so the final request must be the shortest path. **************************************** * ***********/# Include <stdio. h> # include <math. h> # include <string. h >#include <algorithm> # include <iostream> using namespace STD; const int maxn = 110; int n, m; struct point {Double X, Y, Z; double R;} p [maxn]; int f [maxn];/** parent */struct edge {int U, V; double W;} edge [maxn * maxn]; bool CMP (edge L1, edge l2) {return l1.w <l2.w;} double dist (point a, point B) {return SQRT (. x-B. X) * (. x-B.x) +. y-B.y) * (. y-B.y) +. z-B.z) * (. z-B.z);} int find (int x)/** and query the set find */{return x = f [x]? X: F [x] = find (F [x]);} double Kruskal ()/** the legendary Kruskal algorithm, orz */{double ans = 0; For (INT I = 0; I <m; I ++) /* the edges traversed after sorting must be small to large */{int u = find (edge [I]. u);/** find ancestor */INT v = find (edge [I]. v); If (u = V) continue;/** same ancestor, belongs to the same connected component */else/** belongs to different connected components, merge */{ans + = edge [I]. w; F [u] = V;} return ans;} int main () {While (scanf ("% d", & N )! = EOF) {If (n = 0) break; For (INT I = 0; I <n; I ++) {scanf ("% lf", & P [I]. x, & P [I]. y, & P [I]. z, & P [I]. r); F [I] = I;/** initialize and query the set. You are your own ancestor */} m = 0; /** Number of initialized edges */For (INT I = 0; I <n-1; I ++) {for (Int J = I + 1; j <N; j ++) {edge [M]. U = I; edge [M]. V = J; edge [M]. W = max (0.0, dist (P [I], p [J])-P [I]. r-p [J]. r);/** if two circles intersect, the distance is defined as 0 */M ++;} Sort (edge, edge + M, CMP ); /** sort edges in ascending order of length */Double ans = Kruskal (); printf ("%. 3lf \ n ", ANS);} return 0 ;}
Prime
/*************************************** * *********** Accepted252 kb16 MSC ++ 1419 B2013-07-26 21:32:40: in 3D space, we will give you the coordinates and radius of N sphere. If these sphere do not communicate with each other, you need to establish some channels to connect all the sphere. The connection can be considered when the surface is tangent. Algorithm: min Spanning Tree prime complexity O (N * n) Train of Thought: Classic min Spanning Tree questions first create a graph between each sphere and add an empty connected component from the first vertex, find the closest vertex of the first vertex. At this time, there are two points in the component together. Then, the system keeps searching for a point that is not closest to the connected component in the connected component until all the points are added to the connected component. because each time the connected component is closest to the connected component point, therefore, the result must be the best ********************************* * ******************/# include <stdio. h> # include <math. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 110; const double DNF = 3000; double W [maxn] [maxn]; double d [maxn]; int vis [maxn]; int N; struct point {Double X, Y, Z; Double R;} p [maxn]; double dist (point a, point B) {Return SQRT (. x-B.x) * (. x-B.x) +. y-B.y) * (. y-B.y) +. z-B.z) * (. z-B.z);} double prime () {double ans = 0; For (INT I = 0; I <n; I ++) d [I] = DNF; d [0] = 0;/** connected component of the first vertex */memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <n; I ++) {int X; double M = DNF;/** continuously find the point with the smallest distance component */For (INT y = 0; Y <n; y ++) if (! Vis [y] & D [y] <= m) M = d [x = y]; vis [x] = 1; /** mark the connected component */ANS + = d [X];/** Add the total path */For (INT y = 0; y <n; y ++) if (! Vis [y])/** continuously updates the shortest distance between the points that are not added to the connected component and the connected component */d [y] = min (d [Y], W [x] [Y]);} return ans;} int main () {While (scanf ("% d", & N )! = EOF) {If (n = 0) break; For (INT I = 0; I <n; I ++) for (Int J = 0; j <N; j ++) W [I] [J] = DNF; For (INT I = 0; I <n; I ++) scanf ("% lf", & P [I]. x, & P [I]. y, & P [I]. z, & P [I]. r); For (INT I = 0; I <n; I ++) {for (Int J = I; j <n; j ++) {if (I = J) W [I] [J] = 0; else W [I] [J] = max (0.0, dist (P [I], P [J])-P [I]. r-p [J]. r); W [J] [I] = W [I] [J] ;}} printf ("%. 3lf \ n ", prime ();} return 0 ;}