题意:
给出一些球,球内的时间为零,球之间的速度为10每单位。
给两个点,求最短时间。
思路:
所有点看做洞,起点和终点可以看做r=0的洞,洞到洞的最短距离都是圆心距离减去半径(>=0)。
剩下的就是dijdstra了。
// 最短路
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
const double INF = 1e9;
typedef long long LL;
const int maxn = 100+5;
int holes, n;
int x[maxn], y[maxn], z[maxn], r[maxn];
int done[maxn];
double G[maxn][maxn], d[maxn];
inline LL squ(LL a){ return a*a; }
inline double dist(int i, int j){ return sqrt(squ(x[i] - x[j]) + squ(y[i] - y[j]) + squ(z[i] - z[j])) - r[i] - r[j]; }
struct Node{
double d;
int u;
Node(int a, double b):u(a),d(b){}
bool operator<(const Node& rhs) const{
return d > rhs.d;
}
};
double Dijkstra2(int s, int t){
for(int i = 0; i < n; ++i) {
d[i] = INF; done[i] = 0;
}
d[s] = 0;
priority_queue<Node> Q;
Q.push(Node(s,0));
while(!Q.empty()){
Node t2 = Q.top(); Q.pop();
int x = t2.u;
if(x == t) return d[t];
if(done[x]) continue;
done[x] = 1;
for(int i = 0; i < n; ++i){
if(d[i] > d[x] + G[x][i]){
d[i] = d[x] + G[x][i];
//printf("%d, %lf\n",i+1,d[i]);
Q.push(Node(i, d[i]));
}
}
}
return -1;
}
/*
int vis[maxn];
void Dijkstra(int s){
for(int i = 0; i < n; ++i) {
d[i] = INF; vis[i] = 0;
}
d[s] = 0;
for(int k = 0; k < n; ++k){
double d2 = INF; int u;
for(int i = 0; i < n; ++i) if(!vis[i]&&d2 > d[i]) d2 = d[u = i];
vis[u] = 1;
for(int i = 0; i < n; ++i) d[i] = min(d[i], d[u]+G[u][i]);
}
}*/
int main()
{
freopen("in.txt","r",stdin);
int kase = 1;
while(scanf("%d",&holes) == 1){
if(holes == -1) break;
for(int i = 1; i <= holes; ++i) scanf("%d%d%d%d",x+i, y+i, z+i, r+i);
scanf("%d%d%d",x, y, z); r[0] = 0; // 起点
n = holes + 2;
scanf("%d%d%d",&x[n-1], &y[n-1], &z[n-1]); r[n-1] = 0; // 终点
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
G[i][j] = max(0.0, dist(i, j)); // 和 0 取最大!!!
G[j][i] = G[i][j];
//printf("%d -> %d = %lf\n",i+1, j+1, G[i][j]);
}
}
//Dijkstra(0);
//double ans = d[n-1];
double ans = Dijkstra2(0, n-1);
//for(int i = 0; i <= n; ++i) print("%lf ",d[i]);
printf("Cheese %d: Travel time = %.0lf sec\n", kase++,ans*10);
}
return 0;
}