UVA1001 Say Cheese (dijkstra)

如果没有洞,那么任意两点的最短距离就是直线距离,洞里是瞬间的,所以看成一个点就行了(其实点也可以当作半径为0的洞来处理),洞到洞的最短距离都是圆心距离减去半径。剩下的就是求单源最短路径,是完全图,用不加堆优化的dijkstra就行了O(n^2)。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxn = 102;

int x[maxn],y[maxn],z[maxn],r[maxn];

double g[maxn][maxn];
inline ll squ(ll x) { return x*x; }
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]; }


double d[maxn];
double INF = 1e9;
bool vis[maxn];

double dijkstra(int n,int s = 0,int t = 1)
{
    for(int i = 1; i < n; i++) d[i] = INF;
    memset(vis,0,sizeof(bool)*n);
    for(int i = 0; i < n; i++){
        int x; double m = INF;
        for(int y = 0; y < n; y++) if(!vis[y] && d[y] < m) m = d[x = y];
        if(x == t) return d[t];
        vis[x] = true;
        for(int y = 0; y < n; y++) d[y] = min(d[y],d[x]+g[x][y]);
    }
    return INF;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n,kas = 0;
    while(scanf("%d",&n),~n){
        n += 2;
        for(int i = 2; i < n; i++) scanf("%d%d%d%d",x+i,y+i,z+i,r+i);
        scanf("%d%d%d%d%d%d",x,y,z,x+1,y+1,z+1);
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++){
                g[i][j] = max(0.,dist(i,j));
                g[j][i] = g[i][j];
            }
        printf("Cheese %d: Travel time = %.0lf sec\n",++kas,dijkstra(n)*10);
    }
    return 0;
}

 

posted @ 2015-08-28 22:27  陈瑞宇  阅读(519)  评论(0编辑  收藏  举报