欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ - 2502 Subway(最短路 • 疯狂CE日记)

程序员文章站 2022-05-22 11:38:09
...

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

题意:

先给出起点和终点的坐标,接下来若干条地铁线,每条地铁线至少经过两个站点,给出每个站点的坐标,以-1, -1为结束。允许随意上下地铁和换线路。已知地铁时速40km,人步行时速10km。两点间距即两点坐标之间的距离,求起点至终点的最短路

思路:

一条地铁线相邻两点之间建边,在所有点之间建步行边

 

PS:

我一开始写的距离函数名是distance,没成想一直报错no type named ‘iterator_category’ in ‘struct node’……

查了才知道是distance引起了重名……(以后再也不用完整单词当函数名了::>_<::CE惨了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <stdlib.h>
using namespace std;
const double inf = 1000000.0;
const int N = 210;

int n;
double dis[N], g[N][N];
bool vis[N];

struct node
{
    int x, y;
    node(int xx, int yy)
    {
        x = xx;
        y = yy;
    }
    node(){}
}s[N];

double distan(node a, node b)
{
    double ans = 1.0 * sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) *(a.y - b.y));
    return ans;
}

void dijk()
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; ++i)
    {
        dis[i] = g[1][i];
    }
    vis[1] = 1;
    dis[1] = 0;
    for(int i = 1; i <= n; ++i)
    {
        int pos;
        double minn = inf;
        for(int j = 1; j <= n; ++j)
        {
            if(!vis[j] && minn > dis[j])
            {
                minn = dis[j];
                pos = j;
            }
        }
        vis[pos] = 1;
        for(int j = 1; j <= n; ++j)
        {
            if(!vis[j] && dis[j] > dis[pos] + g[pos][j])
            {
                dis[j] = dis[pos] + g[pos][j];
            }
        }
    }
    return;
}

int main()
{
    n = 0;
    node ss, tt;
    scanf("%d%d%d%d", &ss.x, &ss.y, &tt.x, &tt.y);
    s[++n].x = ss.x;
    s[n].y = ss.y;
    int xx, yy;
    int str = 1;
    while(scanf("%d%d", &xx, &yy) != EOF)
    {
        if(xx == -1 && yy == -1)
        {
            for(int i = str + 1; i < n; ++i)
            {
                double tmp = 3.0 * distan(s[i], s[i + 1]) / 2000.0;
                g[i][i + 1] = g[i + 1][i] = tmp;
            }
            str = n;
            continue;
        }
        s[++n].x = xx;
        s[n].y = yy;
    }
    s[++n].x = tt.x;
    s[n].y = tt.y;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = i + 1; j <= n; ++j)
        {
            double tmp = 3.0 * distan(s[i], s[j]) / 500.0;
            if(g[i][j] > tmp || g[i][j] == 0)
            {
                g[i][j] = g[j][i] = tmp;
            }
        }
    }
    dijk();
    int ans = (int)(dis[n] + 0.5);
    printf("%d\n", ans);
    return 0;
}

 

相关标签: 图论