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

题解:给出起点、终点的坐标,要从起点走到终点,步行的速度是10k下/h。再给出N条地铁线路,每条给出不小于2个的坐标,表示这些坐标构成一条线路。在地铁轨道中,速度是40km/h。我们认为无论何时你到某个站,都恰好有一辆车来。求起点到终点的最小时间。

这其实是个最短路的问题,对于每一条路线都要记录下相邻的点的时间,但是要对于每两点之间的时间都要更新,可以dijkstra来得出结果。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,book[333];
double e[330][330],dis[330];
struct node
{
    int x,y;
} mp[330];
double lon(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void lin()
{
    int i,j,k;
    for(i=1; i<n; i++)
    {
        double minn=inf;
        for(j=1; j<=n; j++)
        {
            if(book[j]==0&&dis[j]<minn)
            {
                minn=dis[j];
                k=j;
            }
        }
        book[k]=1;
        for(j=1; j<=n; j++)
            if(book[j]==0&&e[k][j]!=inf&&dis[k]+e[k][j]<dis[j])
                dis[j]=dis[k]+e[k][j];
    }
    printf("%.0f\n",dis[2]);
}
int main()
{
    int i,j;
    for(i=0; i<330; i++)
        for(j=0; j<330; j++)
            if(i==j)
                e[i][j]=0;
            else
                e[i][j]=e[j][i]=inf;
    scanf("%d%d%d%d",&mp[1].x,&mp[1].y,&mp[2].x,&mp[2].y);
    int m,tx,ty;
    m=n=3;
    while(~scanf("%d%d",&tx,&ty))
    {
        if(tx==-1&&ty==-1)
        {
            m=n;
            continue;
        }
        mp[n].x=tx;
        mp[n].y=ty;
        if(n!=m)
            e[n][n-1]=e[n-1][n]=lon(mp[n],mp[n-1])*3/2000;
        n++;
    }
    for(i=1; i<=n; i++)
        for(j=i+1; j<=n; j++)
            e[i][j]=e[j][i]=min(e[i][j],lon(mp[i],mp[j])*3/500);
    for(i=1; i<=n; i++)
        dis[i]=e[1][i];
    memset(book,0,sizeof(book));
    book[1]=1;
    lin();
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值