Test instructions
A female mouse wants to find her toy, and the toy is thrown in a wide 3-dimensional space at some point, and the female mouse at another point, she can go directly to the location of the toy, but the time-consuming is the Euclidean distance traveled by *10s. There is another way, is by drilling holes, holes are spherical, in the hole how to walk is not time-consuming. How long does it take for the mother mouse to find her toy?
Analysis:
The shortest distance from the hole to the hole is the center distance minus the radius. All that remains is to seek the shortest path of the single source.
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
using namespace Std;
typedef long Long LL;
const int maxn=102;
int X[MAXN],Y[MAXN],Z[MAXN],R[MAXN];
Double G[MAXN][MAXN];
int VIS[MAXN];
Double D[MAXN];
Double INF = 1e9;
ll Ping (ll x)
{
return x*x;
}
Double Dist (int i,int j)
{
return sqrt (ping (x[i]-x[j]) +ping (Y[i]-y[j]) +ping (Z[i]-z[j])-r[i]-r[j];
}
Double Dijkstra (int n)
{
for (int i=1;i<n;i++)
D[i]=inf;
memset (vis,0,sizeof (VIS));
for (int i=0;i<n;i++)
{
int x;
Double m = INF;
for (int y=0;y<n;y++)
if (!vis[y]&&d[y]<m)
M=d[x=y];
if (x==n-1)
return d[n-1];
Vis[x]=1;
for (int y=0;y<n;y++)
D[y]=min (D[y],d[x]+g[x][y]);
}
return INF;
}
int main ()
{
int n,cas=0;
while (scanf ("%d", &n) &&n>=0)
{
Memset (r,0,sizeof (R));
int i,j;
for (i=1;i<=n;i++)
scanf ("%d%d%d%d", &x[i],&y[i],&z[i],&r[i]);
scanf ("%d%d%d", &x[0],&y[0],&z[0]);
scanf ("%d%d%d", &x[n+1],&y[n+1],&z[n+1]);
<<1<<endl;
for (i=0;i<=n+1;i++)
{
for (j=i+1;j<=n+1;j++)
{
G[i][j]=max (0.0,dist (i,j));
G[J][I]=G[I][J];
}
}
printf ("Cheese%d:travel time =%.0lf sec\n", ++cas,dijkstra (n+2) *10);
}
}
UVA 1001 Say Cheese