题意:给定一个三维空间的一些球和起始位置和结束位置,问你最短要花的时间是多少。
析:建图,所有的位置都建立图,边权就是距离,最小求一次最短路即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<double, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-5; const int maxn = 100 + 10; const int mod = 1e6; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Point{ double x, y, z, r; }; Point a[maxn]; double G[maxn][maxn]; double d[maxn]; double distant(int i, int j){ return max(0.0, sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y) + (a[i].z-a[j].z)*(a[i].z-a[j].z)) - a[i].r - a[j].r); } double dijkstra(int s, int t){ priority_queue<P, vector<P>, greater<P> >pq; fill(d, d+n+5, INF); d[s] = 0.0; pq.push(P(0.0, s)); while(!pq.empty()){ P p = pq.top(); pq.pop(); if(p.second == t) return p.first; int u = p.second; if(d[u] < p.first) continue; for(int i = 0; i < n+2; ++i){ if(d[i] > d[u] + G[u][i]){ d[i] = d[u] + G[u][i]; pq.push(P(d[i], i)); } } } } int main(){ int kase = 0; while(scanf("%d", &n) == 1 && n >= 0){ for(int i = 0; i < n; ++i) scanf("%lf %lf %lf %lf", &a[i].x, &a[i].y, &a[i].z, &a[i].r); int s = n, t = n+1; for(int i = n; i < n+2; ++i) scanf("%lf %lf %lf", &a[i].x, &a[i].y, &a[i].z); a[n].r = a[n+1].r = 0.0; for(int i = 0; i < n+2; ++i) for(int j = i+1; j < n+2; ++j) G[i][j] = G[j][i] = distant(i, j); double ans = dijkstra(s, t) * 10; printf("Cheese %d: Travel time = %.f sec\n", ++kase, ans); } return 0; }