题目大意是给定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;
}