POJ 2505 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

大致题意
一开始给出两个点表示家和学校的坐标位置,之后给出多行数据(没有说几行,以EOF结束)每行以(-1,-1)的坐标表示结束,前面一串数字,每两个数字表示一个地铁站的地铁坐标,这一行地铁都是双向相同的,地铁的坐标是m为单位,问从家开始,如果已知步行速度是10km/h,地铁是40km/s,问从家到学校最短需要几分钟(结果四舍五入)(坐标可能存在负数,但不存在 [-1,-1] 坐标)

具体思路
毒瘤输入,最多就200个地铁站,加上家和学校开个205的数组就够了,同一条地铁路线在输入时候就计算相邻两点之间的时间,其他点与点之间用for循环进行步行时间的计算,最短路可以dij可以spfa也可以floyd,都不会炸时间,要注意的是结果要四舍五入

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
typedef long long int LLD;
struct Node
{
    LLD x,y;
}node[205];
double line[205][205];
double cal(LLD from,LLD to,LLD s)
{
    return sqrt(pow((node[from].x-node[to].x),2)+pow((node[from].y-node[to].y),2))/(1.0*s/60);
}
int main()
{
    LLD homex,homey,schoolx,schooly,wayx,wayy;
    scanf("%lld %lld %lld %lld",&homex,&homey,&schoolx,&schooly);
    node[1].x=homex;
    node[1].y=homey;
    LLD sum=2,num=0;
    fill(line[0],line[0]+205*205,0x7fffffff);
    while (scanf("%lld %lld",&wayx,&wayy)!=EOF)
    {
        if (wayx==-1&&wayy==-1)
        {
            num=0;
        }else
        {
            node[sum].x=wayx;
            node[sum].y=wayy;
            if (num)
            {
                line[sum][sum-1]=line[sum-1][sum]=cal(sum,sum-1,40000);
            }
            num++;
            sum++;
        }

    }
    node[sum].x=schoolx;
    node[sum].y=schooly;
    for (LLD i=1;i<=sum;i++)
    {
        for (LLD j=1;j<=sum;j++)
        {
            if (i==j)
            {
                line[i][j]=line[j][i]=0;
            }else
            {
                if (line[i][j]==0x7fffffff)
                    line[i][j]=line[j][i]=cal(i,j,10000);
            }
        }
    }
    for (LLD k=1;k<=sum;k++)
    {
        for (LLD i=1;i<=sum;i++)
        {
            for (LLD j=1;j<=sum;j++)
            {
                if(line[i][k]+line[k][j]<line[i][j])
                {
                    line[i][j]=line[i][k]+line[k][j];
                }
            }
        }
    }
    if (line[1][sum]-(LLD)line[1][sum]>=0.5)
    {
        printf("%lld\n",(LLD)line[1][sum]+1);
    }else
    {
        printf("%lld\n",(LLD)line[1][sum]);
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值