Subway POJ - 2502

问题:

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)表示这个地铁站点输入结束,地铁可以双向通行,步行速度为每小时10千米,地铁为每小时40千米,问你从家到学校最快可以用多少分钟,保留整数,(长度单位 米,且假设当你走到站台时刚好可以坐上地铁)。

思路:

最短路问题,把你家,学校,站点的坐标记录下来,求出相互到达需要的时间,采用邻接矩阵存储,然后在用Dijkstra算法求解,但是这个题的预处理还是非常烦人的。

代码:

#define N 2010
#define inf 0x3f3f3f3f
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
double k[N][N];
double s[N][2];
double dis[N];
bool book[N];
double qqq(int x,int y)//计算连点之间的距离
{
    return sqrt(pow((s[x][0]-s[y][0]),2.0)+pow((s[x][1]-s[y][1]),2.0));
}
int main()
{
    double fa=10000.0/60.0,fb=40000.0/60.0;//题意时求分钟,所以要把km/h化成m/分钟
    for(int i=0; i<=N; i++)//数组初始化
    {
        k[i][i]=0;
        for(int j=i+1; j<=N; j++)
            k[i][j]=k[j][i]=inf;
    }
    //把你家看成1号顶点,学校看成2号顶点,然后站点从三号顶点以次增加
    scanf("%lf%lf%lf%lf",&s[1][0],&s[1][1],&s[2][0],&s[2][1]);
    int a=2,b=3;
    double x,y;
    while(~scanf("%lf%lf",&x,&y))
    {
        if(x==-1||y==-1)//这段地铁线路输入结束
        {
            b=a+1;
            continue;
        }
        a++;
        s[a][0]=x;
        s[a][1]=y;
        if(a!=b)
            k[a-1][a]=k[a][a-1]=min(k[a-1][a],qqq(a-1,a)/fb);//计算同一地铁相邻站点之间所用时间
    }
    for(int i=1; i<=a; i++)//计算不同站点之间或与学校,你家之间步行所用时间
        for(int j=i+1; j<=a; j++)
            k[i][j]=k[j][i]=min(k[i][j],qqq(i,j)/fa);
    //Dijkstra算法
    for(int i=1; i<=a; i++)
        dis[i]=inf;
    memset(book,false,sizeof(book));
    dis[1]=0;
    for(int i=1; i<=a; i++)
    {
        int u=0;
        double minn=inf;
        for(int j=1; j<=a; j++)
        {
            if(!book[j]&&dis[j]<minn)
            {
                minn=dis[j];
                u=j;
            }
        }
        book[u]=true;
        for(int v=1; v<=a; v++)
        {
            if(!book[v]&&dis[v]>dis[u]+k[u][v])
                dis[v]=dis[u]+k[u][v];
        }
    }
    printf("%.0f\n",dis[2]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值