当前位置: 首页 > 工具软件 > Subway > 使用案例 >

L - Subway(建图+dijkstra)

裴翰学
2023-12-01

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

大致题意: 给定两个点的坐标, 以及一系列地铁线和地铁站, 给出步行速度和地铁速度, 求两点间来往的最少时间

思路 : 这题难就难在建图, 可以通过结构体来存点, 对相同的地铁线(即相邻的地铁站)打上标记, 对于同意地铁线可以走地铁, 其余点走步行, 便于后续计算两点的时间, 建完图用dijkstra走一遍即可

注: 结果要求四舍五入输出答案, 可以通过%.0lf(这个我不知道为啥我过不了) 或

强制转换 (int)(n+0.5) 来输出

由于本题输出比较特殊, 可以通过输入完数据后, Ctrl+z 后 回车 , 查看输出结果

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
using namespace std;

stringstream ss;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 220;
const int INF = 0x3f3f3f3f;

int n;
double g[N][N],d[N];
double vb = 10.0*1000.0/60.0, vs = 40.0*1000.0/60.0; // 单位转换
bool st[N];

struct Point{
    int x,y;
    int idx; // idx为标记
}p[N];

double dis(double x1, double y1, double x2, double y2)
{
    return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}

void dijkstra()
{
    for(int i = 1; i<=n; i++) d[i] = g[1][i];
    d[1] = 0;
    for(int i = 1; i<=n; i++)
    {
        int t = -1;
        for(int j = 1; j<=n; j++)
        {
            if(!st[j] && (t == -1 || d[j]<d[t])) t  =j;
        }
        st[t] = 1;
        for(int j = 1; j<=n; j++)
        {
            d[j] = min(d[j], d[t] + g[t][j]);
        }
    }
}
int main()
{
    scanf("%d %d %d %d",&p[1].x, &p[1].y, &p[2].x, &p[2].y);
    p[1].idx = 1;
    p[2].idx = 2;
    int cnt = 3, idx = 3;
    while(~(scanf("%d %d", &p[cnt].x, &p[cnt].y)))
    {
        p[cnt].idx = idx;
        cnt++;
        while((scanf("%d %d", &p[cnt].x, &p[cnt].y)) && p[cnt].x != -1)
        {
            p[cnt].idx = idx;
            cnt++;
        }
        idx++;
    }
    n = cnt-1;
    for(int i = 1; i<=n; i++)
    {
        for(int j = 1; j<=n; j++)
        {
            if(i == j) g[i][j] = 0;
            else g[i][j] = INF;
        }
    }
    for(int i = 1; i<=n; i++)
    {
        for(int j = i+1; j<=n; j++)
        {
            double dist = dis(p[i].x,p[i].y,p[j].x,p[j].y), t;
            if(j == i+1 && p[i].idx == p[j].idx)
            {
                t = dist/(1.0*vs);
            }else t = dist/(1.0*vb);
            g[i][j] = g[j][i] = min(t, g[i][j]);
        }
    }
    dijkstra();
//    printf("%.0lf\n", d[2]); // 小小的0.5, 大大的坐牢
    printf("%d\n",(int)(d[2]+0.5));
}

 类似资料: