UVA 1001 Say Cheese(最短路)

题意:阅读题....无限大的奶酪里有n个球形的洞。你的任务是帮助老鼠A用最短时间到达老鼠O的位置。奶酪里面的移动速度为10S一个单位,但是在洞里可以瞬间移动,求最短时间

思路:先耐心看题目...然后就floyd随便做了


#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
int x[maxn],y[maxn],z[maxn],r[maxn];
double d[maxn][maxn];

int main()
{
    int n,cas=1;
	while (scanf("%d",&n)!=EOF && n!=-1)
	{
		for (int i = 1;i<=n+2;i++)
		{
			if (i<=n)
				scanf("%d%d%d%d",&x[i],&y[i],&z[i],&r[i]);
			else
				scanf("%d%d%d",&x[i],&y[i],&z[i]),r[i]=0;
		}
		n+=2;
		for (int i = 1;i<=n;i++)
		for (int j = 1;j<=n;j++)
		{
			if (i==j)
				d[i][j]=0;
			else
			{
				double dist = sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j])+(double)(z[i]-z[j])*(z[i]-z[j]));
				if (dist<=r[i]+r[j])
					d[i][j]=0;
				else
					d[i][j]=dist-(r[i]+r[j]);
			}
		}
        for (int k = 1;k<=n;k++)
			for (int i = 1;i<=n;i++)
				for (int j =1;j<=n;j++)
					d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		printf("Cheese %d: Travel time = %.0lf sec\n",cas++,d[n-1][n]*10);
	}
}


Once upon a time, in a giant piece of cheese, there lived a cheese mite named Amelia Cheese Mite.
Amelia should have been truly happy because she was surrounded by more delicious cheese than she
could ever eat. Nevertheless, she felt that something was missing from her life.
One morning, her dreams about cheese were interrupted by a noise she had never heard before. But
she immediately realized what it was - the sound of a male cheese mite, gnawing in the same piece of
cheese! (Determining the gender of a cheese mite just by the sound of its gnawing is by no means easy,
but all cheese mites can do it. That’s because their parents could.)
Nothing could stop Amelia now. She had to meet that other mite as soon as possible. Therefore
she had to find the fastest way to get to the other mite. Amelia can gnaw through one millimeter of
cheese in ten seconds. But it turns out that the direct way to the other mite might not be the fastest
one. The cheese that Amelia lives in is full of holes. These holes, which are bubbles of air trapped
in the cheese, are spherical for the most part. But occasionally these spherical holes overlap, creating
compound holes of all kinds of shapes. Passing through a hole in the cheese takes Amelia essentially
zero time, since she can fly from one end to the other instantly. So it might be useful to travel through
holes to get to the other mite quickly.
For this problem, you have to write a program that, given the locations of both mites and the holes
in the cheese, determines the minimal time it takes Amelia to reach the other mite. For the purposes of
this problem, you can assume that the cheese is infinitely large. This is because the cheese is so large
that it never pays for Amelia to leave the cheese to reach the other mite (especially since cheese-mite
eaters might eat her). You can also assume that the other mite is eagerly anticipating Amelia’s arrival
and will not move while Amelia is underway.
Input
The input file contains descriptions of several cheese mite test cases. Each test case starts with a line
containing a single integer n (0 ≤ n ≤ 100), the number of holes in the cheese. This is followed by
n lines containing four integers xi
, yi
, zi
, ri each. These describe the centers (xi
, yi
, zi) and radii ri
(ri > 0) of the holes. All values here (and in the following) are given in millimeters.
The description concludes with two lines containing three integers each. The first line contains the
values xA, yA, zA, giving Amelia’s position in the cheese, the second line containing xO, yO, zO, gives
the position of the other mite.
The input file is terminated by a line containing the number ‘-1’.
Output
For each test case, print one line of output, following the format of the sample output. First print the
number of the test case (starting with 1). Then print the minimum time in seconds it takes Amelia
to reach the other mite, rounded to the closest integer. The input will be such that the rounding is
unambiguous.

Sample Input
1
20 20 20 1
0 0 0
0 0 10
1
5 0 0 4
0 0 0
10 0 0
-1
Sample Output
Cheese 1: Travel time = 100 sec
Cheese 2: Travel time = 20 sec

你可能感兴趣的:(UVA 1001 Say Cheese(最短路))