poj 2502 Subway

http://poj.org/problem?id=2502

Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7886   Accepted: 2545

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

题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)

解题思路:这个主要是卡的输入,只要将输入弄好了,构成一张图就好做了,然后需要注意的是,对于地铁线,你从1站到2站的速度是40km/h,但是不代表你从1站到3站的速度是40km/h,你必须经过2站才能到3站,然后我们构图的时候权值保存的是时间多少分钟(可能会有从a到b点有多个分钟,取最小的),这里单位换算也要注意下,然后就是直接模版了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>

using namespace std;

#define N 300
#define INF 0x3f3f3f3f
#define met(a, b) memset (a, b, sizeof (a))

struct node
{
    double x, y;
}val[N];

double g[N][N], dist[N];
int vis[N], cnt;

void Dij ()
{
    for (int i=0; i<=cnt; i++)
        dist[i] = INF;
    for (int i=1; i<=cnt; i++)
    {
        vis[i] = 0;
        dist[i] = min (g[1][i], dist[i]);
    }

    vis[1] = 1;
    dist[1] = 0;

    for (int i=1; i<=cnt; i++)
    {
        double minx = INF;
        int Index = 0;
        for (int j=1; j<=cnt; j++)
            if (!vis[j] && dist[j] < minx)
                minx = dist[Index = j];
        vis[Index] = 1;
        for (int j=1; j<=cnt; j++)
            if (!vis[j] && dist[j] > dist[Index] + g[Index][j])
                dist[j] = dist[Index] + g[Index][j];
    }
    printf ("%.0f\n", dist[cnt]);
}

int main ()
{
    double x1, y1, x2, y2;
    while (scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) != EOF)
    {
        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;
            }

        met (val, 0);
        val[1] = (node) {x1, y1};

        cnt = 2;
        int k = 2;

        while (scanf ("%lf %lf", &val[k].x, &val[k].y) != EOF)
        {
            if (val[k].x ==-1 && val[k].y == -1)
            {
                cnt = k;
                continue;
            }

            if (cnt != k)
            g[k][k-1] = g[k-1][k] = min (g[k-1][k], sqrt((val[k-1].x-val[k].x)*(val[k-1].x-val[k].x)+(val[k-1].y-val[k].y)*(val[k-1].y-val[k].y))/40000.0*60);

            k++;
        }

        val[cnt] = (node) {x2, y2};

        for (int i=1; i<=cnt; i++)
            for (int j=1; j<=cnt; j++)
                g[i][j] = min (g[i][j], sqrt((val[i].x-val[j].x)*(val[i].x-val[j].x)+(val[i].y-val[j].y)*(val[i].y-val[j].y))/10000.0*60);
        Dij();
    }
    return 0;
}


你可能感兴趣的:(poj 2502 Subway)