POJ-2502 Subway(最短路 Dijkstra)

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

题意:某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度10km/h,地铁速度40km/h,给出学生家和学校以及每条地铁的站点坐标,求学生从家到学校的最短时间。

注意:单位站点距离的单位是m,速度单位应换算成m/min;

思路:最短路的应用,用Dijkstra算法,建图时每一条地铁之间的站点建边用地铁的速度算时间,本条地铁与其他地铁站点之间用步行算时间,建边过程比较复杂;

代码:

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define INF 0x3f3f3f3f
#define Max 300

struct Point
{
    int x,y;
}dkr[Max];

double dis[Max],Map[Max][Max];//Map[][]存时间 
int vis[Max],n;

void dijkstra(int x)
{
    int i,j,k,u;
    for( i=1 ; i<=n ; i++ )
    {
        dis[i]=Map[x][i];
        vis[i]=0;
    }
    vis[x]=1;
    for( i=1 ; i<n ; i++ )
    {
        double minn=INF;//注意double型 
        for( j=1 ; j<=n ; j++)
        {
            if(!vis[j] && minn>dis[j] )
            {
                minn=dis[j];
                u=j;
            }
        }
        vis[u]=1;
        for( j=1 ; j<=n ; j++ )
        {
            if(!vis[j]&&Map[u][j]!=INF&& dis[j] > dis[u]+Map[u][j])
            {
                dis[j]=dis[u]+Map[u][j];
            }
        }
    }
    printf("%.0f",dis[2]);
}

double feet(Point t,Point s)
{
    return sqrt((t.x-s.x)*(t.x-s.x)+(t.y-s.y)*(t.y-s.y));//两点之间距离 
}

int main()
{
    int i,j,k=3;//k现在赋值第一条地铁的首个地铁站 
    for( i=1 ; i<Max ; i++ )
        for( j=1 ; j<Max ; j++ )
            if(i==j)
                Map[i][j] = 0 ;
            else
                Map[i][j] = INF ;
    scanf("%d%d%d%d",&dkr[1].x ,&dkr[1].y,&dkr[2].x ,&dkr[2].y);//起点,终点 
    n=3;
    int x,y;
    while(~scanf("%d%d",&x,&y))//地铁 
    {
        if(x==-1&&y==-1)
        {
           k=n;
           continue;
        }
        dkr[n].x=x;
        dkr[n].y=y;
        if(n!=k)//本条地铁的首个站点不能与相邻地铁站建边 
            Map[n][n-1]=Map[n-1][n]=feet(dkr[n],dkr[n-1])*3/2000;
        n++;
    }
    for( i=1 ; i<=n ;i++)//步行 
    {
        for( j=1 ; j<=n ; j++ )
        {
            Map[i][j]=Map[j][i]=min(Map[i][j],feet(dkr[i],dkr[j])*3/500);
        }
    }
    dijkstra(1);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值