A - Say Cheese (最短路)

问题描述:

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 shecould 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. Butshe immediately realized what it was – the sound of a male cheese mite, gnawing in the same piece ofcheese! (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. Thereforeshe had to find the fastest way to get to the other mite. Amelia can gnaw through one millimeter ofcheese in ten seconds. But it turns out that the direct way to the other mite might not be the fastestone. The cheese that Amelia lives in is full of holes. These holes, which are bubbles of air trappedin the cheese, are spherical for the most part. But occasionally these spherical holes overlap, creatingcompound holes of all kinds of shapes. Passing through a hole in the cheese takes Amelia essentiallyzero time, since she can fly from one end to the other instantly. So it might be useful to travel throughholes 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 holesin the cheese, determines the minimal time it takes Amelia to reach the other mite. For the purposes ofthis problem, you can assume that the cheese is infinitely large. This is because the cheese is so largethat it never pays for Amelia to leave the cheese to reach the other mite (especially since cheese-miteeaters might eat her). You can also assume that the other mite is eagerly anticipating Amelia’s arrivaland 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 linecontaining a single integer n (0 ≤ n ≤ 100), the number of holes in the cheese. This is followed byn 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 thevalues xA, yA, zA, giving Amelia’s position in the cheese, the second line containing xO, yO, zO, givesthe 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 thenumber of the test case (starting with 1). Then print the minimum time in seconds it takes Ameliato reach the other mite, rounded to the closest integer. The input will be such that the rounding isunambiguous.


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

题目大意:

母老鼠要寻找它的公老鼠玩具,玩具在一个广阔的三维空间中,母老鼠的初始位置也在三维空间中,他直接走到玩具的位置耗时是所有的距离*10s;还有一种方法就是钻洞,洞是球的,在洞内怎么走都不消耗时间,求耗时。

思路:可以把两只老鼠的位置看成球半径为0.

然后两点之间的最短距离就是两球心之间的距离-两球的半径。如果两球半径和大于球心距的话就表明这两点之间距离为0。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxn 110
#define LL long long
int x[maxn],y[maxn],z[maxn],r[maxn];
double dist[maxn][maxn];
int main()
{
    int n,j=0;
    while(scanf("%d",&n),n>=0)
    {
        memset(r,0,sizeof(r));//起点和终点的半径为0
        for(int 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]);
        //求两两之间的距离
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<=n+1;j++)
            {
                dist[i][j]=0;
                if(j!=i)
                {
                    dist[i][j]=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)+pow(z[i]-z[j],2))-r[i]-r[j];//求两点间的距离建图
                    if(dist[i][j]<1e-7) dist[i][j]=0;
                }
            }
        }
        //Floyd
         for(int k=0; k<=n+1; k++)
            for(int i=0; i<=n+1; i++)
                for(int j=0; j<=n+1; j++)
                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
        printf("Cheese %d: Travel time = %lld sec\n", ++j, (LL)(round(dist[0][n+1]*10)+0.5) );

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值