Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5863 | Accepted: 2905 |
Description
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 should 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) should 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 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 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
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 is positive and is less than 100.0.
The end of the input is indicated by a line containing a zero.
Output
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
3 10.000 10.000 50.000 10.000 40.000 10.000 50.000 10.000 40.000 40.000 50.000 10.000 2 30.000 30.000 30.000 20.000 40.000 40.000 40.000 20.000 5 5.729 15.143 3.996 25.837 6.013 14.372 4.818 10.671 80.115 63.292 84.477 15.120 64.095 80.924 70.029 14.881 39.472 85.116 71.369 5.553 0
Sample Output
20.000 0.000 73.834
看懂题意就会发现这个题就是求最小生成树的,只是多了一维坐标而已,另外距离的处理要算上两个房间的半径,如果算出的距离为负值的话,那么就要设置为零(已经连通),其他地方就没有难点了,主要是读懂题意
keruscal 算法....
算法简析:
最小生成树的krusal算法,主要利用并查集和贪心的思想来生成一棵权值最小的生成树(最小生成树),为了把所有的顶点连接起来,而且总的权值最短,那么按贪心的思路,肯定要先把所有的权值都统计下来,从权值最小的来考虑,而且为了使得n个顶点全部连接起来,那么只需要n-1条路,每一条路,都连接着两条不同的城市(也就是不能两个城市之间直接相连多条路),而且,不同的城市之间只需要有唯一一条边可以到达任意一个顶点即可,也就是不能有环,然后下面就是如何处理了,为了使得上述情况符合要求,那么我们每次在连接两个顶点的时候,都需要判断这两个顶点是否已经连接在一起了,如果没有直接或者间接地连接在一起,那么就需要把这两个顶点连接在一起,使得这些顶点之间可以相互到达,否则的话,证明这条路已经不需要了,比如 三个顶点,如果已经满足:1可以到达2,2可以到达3,那么在下一次连接的时候,如果有条边是 2到达3, 也就是2 3 这两个点已经可以连通,那么这条路就不需要了(具体怎么实现,继续往下看),因为多了一条路,肯定会引起权值变大,至于具体哪条路不需要,这就是要用贪心的思想了:把所有的边的长度都由小到大排序,每次都选择最短的,那么在最后,把所有的顶点全部连接起来的时候,肯定能满足即没有多余的边,又保证每两个城市直接相连的边的权值最小,也就是最小生成树已经生成!
这个算法的核心是:如何判断某两个顶点是否已经连通?有一种数据结构叫并查集,这里用来解决这个问题最合适不过了,这里不赘述并查集的实现方法,只说明一下并查集的功能:并查集是一种能判断数据之间是否有相关关系,或者说是否在一个集合的一种数据结构。使用并查集可以很高效的实现kruscal 算法。
因为这个算法处理的是边,这个题给出的每个顶点的坐标,需要我们求最小的总权值,那么我们需要先计算每两个顶点之间的边的长度,注意这个题的题意:需要注意顶点的半径,也就是两个顶点的距离减去两个顶点的半径才是他们的距离,另外,如果距离为负值,当成零处理。
ps:今天的一道周赛题,前期没看懂题意,后来马上要解决出来了,时间到了,心塞....
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int per[105],n,m;
double a[105],b[105],c[105],d[105];//分别存三个坐标和直径
struct lu
{
int a,b;
double len;
}x[10005];
double dist(int i,int j)
{
return sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j])+(c[i]-c[j])*(c[i]-c[j]))-d[i]-d[j];//计算距离有点复杂
}
int cmp(lu a,lu b)
{
return a.len<b.len;
}
void init()//初始化
{
for(int i=0;i<n;++i)
{
per[i]=i;
}
}
int find(int x)
{
int r=x;
while(per[r]!=r)
{
r=per[r];
}
int i=x,j;
while(i!=r)
{
j=per[i];per[i]=r;i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return 1;
}
return 0;
}
void kruscal()
{
init();
int cnt=0;double sum=0;
for(int i=0;cnt<n-1;++i)//加边控制
{
if(join(x[i].a,x[i].b))
{
sum+=x[i].len;
++cnt;
}
}
printf("%.3lf\n",sum);
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;++i)
{
scanf("%lf%lf%lf%lf",a+i,b+i,c+i,d+i);
}
int cnt=0;
for(int i=0;i<n-1;++i)//统计所有的边
{
for(int j=i+1;j<n;++j)
{
x[cnt].a=i;x[cnt].b=j;
x[cnt].len=dist(i,j);
if(x[cnt].len<0)//注意为负值的情况
{
x[cnt].len=0;
}
++cnt;
}
}
sort(x,x+cnt,cmp);//按从小到大排序...
kruscal();
}
return 0;
}