Problem Description
Problem Analysis
Given the sphere centers (x, y, z) and radii (r), if two spheres are disjoint, the distance between them is the distance between their centers minus the two radii; otherwise the distance between them is 0
All points must be connected, and the minimum total distance must be output.
It feels like a 3D version of
【HDU】[1875]Smooth Traffic Project Continued
though it seems a bit simpler than that one.
The idea is still to derive edge lengths from the points.
Note that if the two spheres are not disjoint, the distance is 0;
if they are disjoint, it is the shortest distance between the two surfaces.
Remember to subtract the radii from the distance between the two points.
English is still my weak spot…
Also, I don’t know why C++ passes…
Many G++ submissions got WA…
Looking at the algorithm…
I’ll leave this issue aside for now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| #include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct xyz {
double x,y,z,r;
} a[120];
int main() {
double dis[120];
double e[120][120];
bool flag[120];
double inf=999999;
int N;
while(scanf("%d",&N),N) {
for(int i=1; i<=N; i++) {
dis[i]=inf;
flag[i]=false;
}
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
e[i][j]=inf;
}
}
for(int i=1; i<=N; i++) {
scanf("%lf %lf %lf %lf",&a[i].x,&a[i].y,&a[i].z,&a[i].r);
for(int j=1; j<=N; j++) {
if(i==j)
continue;
else {
double l=sqrt(pow(a[i].x-a[j].x,2)+pow(a[i].y-a[j].y,2)+pow(a[i].z-a[j].z,2));
if(l>a[i].r+a[j].r) {
e[j][i]=e[i][j]=l-a[i].r-a[j].r;
} else {
e[j][i]=e[i][j]=0;
}
}
}
}
double sum=0;
dis[1]=0;
while(true) {
int v=-1;
for(int u=1; u<=N; u++) {
if(!flag[u]&&(v==-1||dis[u]<dis[v]))
v=u;
}
if(v==-1)
break;
flag[v]=true;
sum+=dis[v];
for(int u=1; u<=N; u++) {
dis[u]=min(dis[u],e[v][u]);
}
}
printf("%.3lf\n",sum);
}
return 0;
}
|
Problem link: 【POJ】[2031]Building a Space Station