POJ 2031 Building a Space station

Source: Internet
Author: User
Tags x2 y2

Building a Space stationtime limit:1000msmemory limit:30000kbthis problem would be judged onPKU. Original id:2031
64-bit integer IO format: %lld Java class name: Main 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.
InputThe 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.
OutputFor 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
SourceJapan 2003 domestic Problem Solving: ... Mst
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <cmath>6 #defineINF 0x3f3f3f3f7 #definePII pair<double,int>8 using namespacestd;9 Const intMAXN = the;Ten structarc{ One     intTo,next; A     DoubleCost ; -Arcintx =0,Doubley =0,intz =-1){ -to =x; theCost =y; -Next =Z; -     } -}e[maxn*MAXN]; + intHead[maxn],n,tot; - DoubleX[MAXN],Y[MAXN],Z[MAXN],R[MAXN],D[MAXN]; + BOOLDONE[MAXN]; A voidAddintUintVDoubleW) { atE[tot] =arc (V,w,head[u]); -Head[u] = tot++; - } - DoubleGetdis (intAintb) { -     DoubleTMP = (X[a]-x[b]) * (x[a]-x[b]); -TMP + = (Y[a]-y[b]) * (Y[a]-y[b]); inTMP + = (Z[a]-z[b]) * (Z[a]-z[b]); -TMP =sqrt (TMP); to     returnR[A]+R[B] >= tmp?0.0: tmp-r[a]-R[b]; + } - DoublePrim () { the     DoubleAns =0; *      for(inti =0; i < MAXN; ++i) { $D[i] =INF;Panax NotoginsengDone[i] =false; -     } thepriority_queue< pii,vector< PII >,greater< PII > >Q; +Q.push (Make_pair (d[0] =0,0)); A      while(!Q.empty ()) { the         intU =Q.top (). Second; + Q.pop (); -         if(Done[u])Continue; $Done[u] =true; $Ans + =D[u]; -          for(inti = Head[u]; ~i; i =E[i].next) { -             if(D[e[i].to] >e[i].cost) theQ.push (Make_pair (d[e[i].to] =e[i].cost,e[i].to)); -         }Wuyi     } the     returnans; - } Wu intMain () { -      while(SCANF ("%d",&N), N) { Aboutmemset (head,-1,sizeof(head)); $          for(inti = tot =0; I < n; ++i) { -scanf"%lf%lf%lf%lf", x+i,y+i,z+i,r+i); -         } -          for(inti =0; I < n; ++i) { A              for(intj =0; J < N; ++j) { +                 if(i = = j)Continue; the Add (I,j,getdis (i,j)); -             } $         } theprintf"%.3f\n", Prim ()); the     } the     return 0; the}
View Code

POJ 2031 Building a Space station

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.