UVa 1001

题目大意是给定n个球形的洞,一个起点和一个终点,球体之间可以相交,球体内部是瞬间移动,其余的是一个单位要10sec

时间移动,问最短需要多少时间

思路

floyd

我们把每个球体抽象成一个点,若两个球体相交,则距离是0,若不相交,则两个点之间的距离就是圆心之间的距离减去两个球体的半径

然后把起点和终点当成两个没有半径的球体,最后用floyd求最短路即可。最后算出最短时间。

#include <bits/stdc++.h>
using namespace std;
int n;
struct Ball{
    double x, y, z, r;
    Ball(double x = 0, double y = 0, double z = 0, double r = 0) : x(x), y(y), r(r), z(z){}
};
vector<Ball> ball;
const int maxn = 100 + 10;
double  g[maxn][maxn];
void init(){
    ball.clear();
    memset(g, 0, sizeof(g));
}

#define sqr(x) ((x) * (x))
double dist(Ball a, Ball b){
    return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y) + sqr(a.z - b.z));
}

bool Intersect(int A, int B){
    Ball & a = ball[A], & b = ball[B];
    double d = dist(a, b);
    return !(d > a.r + b.r);
}

int main()
{
    int kase = 0;
    while(scanf("%d", &n) == 1){
        if(n == -1) break;
        init();
        for(int i = 1; i <= n; ++i){
            int x, y, z, r;
            scanf("%d%d%d%d", &x, &y, &z, &r);
            ball.push_back(Ball(x, y, z, r));
        }
        int sx, sy, sz, ex, ey, ez;
        scanf("%d%d%d%d%d%d", &sx, &sy, &sz, &ex, &ey, &ez);
        ball.push_back(Ball(sx, sy, sz, 0));
        ball.push_back(Ball(ex, ey, ez, 0));
        n += 2;
        for(int i = 0; i < n; ++i)
            for(int j = i; j < n; ++j){
                if(i == j) g[i][j] = 0;
                else if(Intersect(i, j)){
                    g[i][j] = g[j][i] = 0;
                }else{
                    g[i][j] = g[j][i] = dist(ball[i], ball[j]) - ball[i].r - ball[j].r;
                }
            }
        for(int k = 0; k < n; ++k)
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < n; ++j)
                    g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
        printf("Cheese %d: Travel time = %d sec\n", ++kase, int(g[n - 1][n - 2] * 10 + 0.5));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值