题意:无限大的奶酪里有n(0<=n<=100)个球形的洞。你的任务是帮助小老鼠A用最短的时间到达小老鼠O所在位置。奶酪里的移动速度为10秒一个单位,但是在洞里可以瞬间移动。洞和洞可以相交。输入n个球的位置和半径,以及A和O的坐标,求最短时间。
分析:
1、因为洞可以相交,所以在计算两点距离时要判断一下if(dist > num[i].r + num[j].r)。
2、两球间的距离为球心间距离-两球半径,起点和终点不是球,可将半径设为0。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 100 + 10; const int MAXT = 10000 + 10; using namespace std; double d[MAXN][MAXN]; struct Point{ int x, y, z, r; void read(){ scanf("%d%d%d", &x, &y, &z); } }num[MAXN]; double getD(Point &A, Point &B){ return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z)); } int main(){ int n; int kase = 0; while(scanf("%d", &n) == 1){ if(n == -1) return 0; memset(d, 0, sizeof d); for(int i = 0; i < n; ++i){ num[i].read(); scanf("%d", &num[i].r); } num[n].read(), num[n].r = 0; num[n + 1].read(), num[n + 1].r = 0; for(int i = 0; i < n + 2; ++i){ for(int j = i + 1; j < n + 2; ++j){ double dist = getD(num[i], num[j]); if(dist > num[i].r + num[j].r){ d[i][j] = d[j][i] = dist - num[i].r - num[j].r; } else{ d[i][j] = d[j][i] = 0; } } } 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]); } } } printf("Cheese %d: Travel time = %.0lf sec\n", ++kase, d[n][n + 1] * 10); } return 0; }
时间: 2024-10-05 21:44:27