POJ - 2502

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10501 Accepted: 3429

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<stdio.h> #include<string.h> #include<math.h> #define N 230 #define INF 0x3f3f3f3f struct p {     double x,y; } q[10010]; double e[N][N],dis[N]; int vis[N],n; void floyd() {     for(int k=0; k<n; k++)         for(int i=0; i<n; i++)             for(int j=0; j<n; j++)                 if(e[i][j]>e[i][k]+e[k][j])                     e[i][j]=e[i][k]+e[k][j];     printf("%.0f\n",e[0][1]); } int main() {     int i,j,m;     scanf("%lf %lf %lf %lf",&q[0].x,&q[0].y,&q[1].x,&q[1].y);

    memset(e,INF,sizeof(e));
    for(i=0; i<=230; i++)
        e[i][i]=0;
    n=2;
    m=0;
    double s;
    while(~scanf("%lf %lf",&q[n].x,&q[n].y))
    {
        if(q[n].x==-1&&q[n].y==-1)
        {
            m=0;
            continue;
        }
        if(!m)//不在一条线上的站点时
        {
            m=1;
            n++;//还需加1,不在一条线上的站点时只能仍步行来弥补,后面还得把他变为步行的(时间)权值
            continue;
        }
        s=sqrt((q[n].x-q[n-1].x)*(q[n].x-q[n-1].x)+(q[n].y-q[n-1].y)*(q[n].y-q[n-1].y));//同一条线上相邻站点的距离
        e[n][n-1]=e[n-1][n]=s*6/4000;//单位换算原型为s*60/40000m(分钟)
        n++;
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)//站点间不能直接到达的,用步行来弥补
        {
            if(e[i][j]==INF)
            {
                s=sqrt((q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
                e[i][j]=e[j][i]=s*6/1000;
            }
        }
    floyd();
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值