poj 2502 Subway 最短路

题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h。现在知道了出发地和终点的坐标,以及这些地铁线路每个 站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发。问你从出发点到终点最少需要多少时间。

分析:按“车能开的地方坐车,车不能开的地方走路”的想法建一个图,结果就是求最短路,因为数据范围很小,所以用floyd算法,没有使用较复杂的单源最短路算法

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
#define inf 999999999
#define re(i,n) for(int i=0;i<n;i++)
const int maxn = 222;
double dist[maxn][maxn];
struct Point {
    double x,y;
}points[maxn];
int n;
double Dis(Point a , Point b) {
    double x = (a.x - b.x) * (a.x - b.x);
    double y = (a.y - b.y) * (a.y - b.y);
    return sqrt(x + y);
}
void floyd() {
    re(k,n) re(i,n) if(i!=k) re(j,n) if(i!=j && j!=k) if(dist[i][j]>dist[i][k]+dist[k][j])
        dist[i][j] = dist[i][k] + dist[k][j];
}
int main() {
    int left;
    double d;
    re(i,maxn) re(j,maxn) dist[i][j] = inf;
    scanf("%lf%lf%lf%lf",&points[0].x,&points[0].y,&points[1].x,&points[1].y);
    left = n = 2;
    while(~scanf("%lf%lf",&points[n].x,&points[n].y)) {
        if(points[n].x==-1 && points[n].y==-1) { left = n; continue; }
        if(left != n) {
            d = 3 * Dis(points[n],points[n-1]) / 2000;
            dist[n][n-1] = dist[n-1][n] = d;
        }
        n++;
    }
    re(i,n) for(int j=i+1;j<n;j++) {
        if(dist[i][j]==inf) {
            d = 3 * Dis(points[i] , points[j]) / 500;
            dist[i][j] = dist[j][i] = d;
        }
    }
    floyd();
    printf("%.0f\n",dist[0][1]);
    return 0;
}
posted @ 2012-07-02 13:05  lenohoo  阅读(335)  评论(0编辑  收藏  举报