poj 2502 Subway

Description

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school. 
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

Input

Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

Output

Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

Sample Input

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1

Sample Output

21

Source

这道题是给出一些站点的坐标,每一条地铁线,给出一些站点,相邻站点之间时间是距离/地铁速度,因为给的坐标单位是米,而题目说的速度是千米每小时,最后答案要求输出分钟,所以速度要转换,需要除以速度,那么就是乘速度的倒数。如此可以确定每条线站点之间的时间,那么有一些距离是需要步行的,所以先把输入的所有的站点记录,并计算好地铁时间,然后用直线距离的步行时间去更新任意两个站点,需要注意的是可能记录了重复的站点,一个站点不一定只属于一条地铁线,所以MAX设的大一些。如此形成一张交通网,需要求的就是七点到终点的最小时间,即最短路问题。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#define inf 0x3f3f3f3f
#define MAX 302
using namespace std;
struct Point {
    double x,y;
}subway[MAX];
double mp[MAX][MAX],dis[MAX];
const double vs = 0.0015,vw = 0.006;///vs是地铁速度的倒数,vw是步行速度的倒数 都是可除尽的。
bool vis[MAX],flag;
int c = 2;
double dist(Point a,Point b) {///两点直线距离
    return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2));
}
int main() {
    scanf("%lf%lf%lf%lf",&subway[0].x,&subway[0].y,&subway[1].x,&subway[1].y);
    for(int i = 0;i < MAX;i ++) {
        for(int j = 0;j < MAX;j ++) {
            mp[i][j] = inf;
        }
        dis[i] = inf;
    }
    while(~scanf("%lf%lf",&subway[c].x,&subway[c].y)) {
        if(subway[c].x == -1 && subway[c].y == -1) {
            flag = 0;
            continue;
        }
        c ++;
        if(flag) {
            mp[c - 1][c - 2] = mp[c - 2][c - 1] = dist(subway[c - 1],subway[c - 2]) * vs;
        }
        else flag = true;
    }
    for(int i = 0;i < c;i ++) {
        for(int j = 0;j < c;j ++) {
            mp[i][j] = mp[j][i] = min(mp[i][j],dist(subway[i],subway[j]) * vw);
        }
    }
    dis[0] = 0;
    while(true) {///dijkstra
        int t = -1;
        double mins = inf;
        for(int i = 0;i < c;i ++) {
            if(!vis[i] && dis[i] < mins) {
                mins = dis[i];
                t = i;
            }
        }
        if(t == -1) break;
        vis[t] = true;
        for(int i = 0;i < c;i ++) {
            if(!vis[i] && dis[i] > mins + mp[t][i]) {
                dis[i] = mins + mp[t][i];
            }
        }
    }
    printf("%.0f",dis[1]);
}
原文地址:https://www.cnblogs.com/8023spz/p/9736031.html