码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2502 Subway(最短路径)

时间:2015-09-03 15:16:51      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

原题地址:http://poj.org/problem?id=2502

 

Subway

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 7347

 

Accepted: 2387

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

 

 

题意:

只有一组数据,第一行给的是家的坐标和学校的坐标,接着后面的都是地铁线路上各个站的坐标,以(-1,-1)结束。已知步行速度为10km/h,地铁速度为40km/h,求家里到学校的最短时间(分钟,四舍五入到整数)。

 

注意:

  1. 给的坐标单位是米,给的速度的单位是km/h,要求的结果是分钟。
  2. 注意在求地铁从1号站到3号站所花费的时间,是从1号站到2号站的距离除以速度,再加上从2号站到3号站的距离除以速度。而不是直接求从1号站到3号站的距离除以速度。(之前就是因为这个WA了几次(-.-!!) )

 

#include <cstdio>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 1000, INF = 0x3f3f3f3f;
struct POINT
{
    int x;
    int y;
}vertex[N];
double time[N][N] = {0};
bool cnt[N] = {0};
void qwe(int& n)
{
    int x, y, start, end;
    while(1)
    {
        start = n;
        for(; ; ++n)
        {
            if(!~scanf("%d %d", &x, &y))
                return ;
            if(x == -1 && y == -1)
            {
                end = n;
                break;
            }
            else
            {
                vertex[n].x = x;
                vertex[n].y = y;
            }
        }
        for(int i=start+1; i<end; ++i)
        {
            double dx = (vertex[i].x - vertex[i-1].x) / 1000.0;
            double dy = (vertex[i].y - vertex[i-1].y) / 1000.0;
            time[i][i-1] = sqrt(dx*dx + dy*dy) * 1.5;
            time[i-1][i] = time[i][i-1];
        }
    }
}
int main(void)
{
    scanf("%d %d %d %d", &vertex[0].x, &vertex[0].y, &vertex[1].x, &vertex[1].y);
    int n = 2, mintag;
    qwe(n);
    for(int i=0; i<n; ++i)
    {
        time[i][i] = INF;
        for(int j=0; j<n; ++j)
        {
            if(time[i][j] == 0)
            {
                double dx = (vertex[i].x - vertex[j].x) / 1000.0;
                double dy = (vertex[i].y - vertex[j].y) / 1000.0;
                time[i][j] = sqrt(dx*dx + dy*dy) * 6;
                time[j][i] = time[i][j];
            }
        }
    }
    cnt[0] = true;
    for(int i=1; i<n; ++i)
    {
        mintag = 0;
        for(int j=1; j<n; ++j)
            if(!cnt[j] && time[0][j] < time[0][mintag])
                mintag = j;
        cnt[mintag] = true;
        for(int j=1; j<n; ++j)
            if(!cnt[j])
                time[0][j] = min(time[0][j], time[0][mintag] + time[mintag][j]);
    }
    printf("%.0lf\n", time[0][1]);
}

 

POJ 2502 Subway(最短路径)

标签:

原文地址:http://www.cnblogs.com/gwtan/p/4780202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!