Uva1001-floyd算法-建图

给出一些球,球内的时间为零,球之间的速度为10每单位。

给两个点,求最短时间。

 

把每一个球当做点,球间的距离就是floyd的d数组。之后跑一遍floyd

 

wa了两发因为d数组定义成int了

 

#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

using namespace std;

const int INF = 100000;

int N,M,T;
double d[110][110];

struct holes
{
    int x,y,z;
    int r;
}hole[110];

double dist(int a,int b)
{
    double ans =  (sqrt(abs(hole[a].x-hole[b].x)*abs(hole[a].x-hole[b].x)+
                        abs(hole[a].y-hole[b].y)*abs(hole[a].y-hole[b].y)+
                        abs(hole[a].z-hole[b].z)*abs(hole[a].z-hole[b].z)
                    ) - (hole[a].r + hole[b].r)) ;
    return ans > 0 ? ans : 0;
}

int main()
{
    while(scanf("%d",&N) && N != -1)
    {
        T++;
        for(int i=0;i<N;i++)
        {
            scanf("%d%d%d%d",&hole[i].x,&hole[i].y,&hole[i].z,&hole[i].r);
        }
        scanf("%d%d%d",&hole[N].x,&hole[N].y,&hole[N].z);
        scanf("%d%d%d",&hole[N+1].x,&hole[N+1].y,&hole[N+1].z);
        hole[N].r = 0;
        hole[N+1].r = 0;

        for(int i=0;i<N+2;i++)
            for(int j=0;j<N+2;j++)
                d[i][j] = (i==j ? 0 : INF);

        for(int i=0;i<N+2;i++)
            for(int j=0;j<N+2;j++)
            {
                if(i != j) d[i][j] = dist(i,j);
            }

        for(int k=0;k<N+2;k++)
            for(int i=0;i<N+2;i++)
                for(int j=0;j<N+2;j++)
                    d[i][j] = min(d[i][j] , d[i][k]+d[k][j]);

        d[N][N+1]*=10;
        int ans = (d[N][N+1]-(int)d[N][N+1]) < (((int)d[N][N+1]+1)-d[N][N+1]) ? (int)d[N][N+1] : (int)d[N][N+1]+1;
        printf("Cheese %d: Travel time = %d sec\n",T,ans);
    }    
}

 

转载于:https://www.cnblogs.com/helica/p/4892391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值