POJ - 2502 Subway

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10410 Accepted: 3384

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


题意:

        你要从waterloo去一个城市。有许多地铁线,每条地铁线包含许多站点。下面给出了每条线路的站点的坐标(以米为单位,以-1,-1结束这条线路)。你要步行去坐地铁,步行的速度为10km/h,地铁速度为40km/h。求到达城市花费的最短时间。

题解:

        这道题输入只有一组数据,不知道有多少条地铁线路,所以要输入到文件结束。可以用dijkstra做。先生成边(权值为花费的时间),用邻接矩阵存储。先生成一条步行起点到终点的边,再生成每条地铁线路站点之间的边。输入完毕后生成不同地铁线路的站点之间的边,速度按照步行的计算。最后直接dijkstra,注意用double存储。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 9999999.0
int begx,begy,endx,endy,cnt;
double mp[210][210],dis[210];
bool vis[210];
struct node
{
    int x,y;
}p[210];
double get(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void dijkstra()
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<cnt;i++)
        dis[i]=mp[0][i];
    vis[0]=1;
    for(int k=1;k<cnt;k++)
    {
        double minn=inf;
        int tmp;
        for(int i=0;i<cnt;i++)
        {
            if(!vis[i] && minn>dis[i])
            {
                minn=dis[i];
                tmp=i;
            }
        }
        vis[tmp]=1;
        for(int i=0;i<cnt;i++)
        {
            if(!vis[i] && dis[i]>dis[tmp]+mp[tmp][i])
                dis[i]=dis[tmp]+mp[tmp][i];
        }
    }
}
int main()
{
    for(int i=0;i<=205;i++)
    {
        for(int j=0;j<=205;j++)
        {
            if(i==j)
                mp[i][j]=0;
            else
                mp[i][j]=inf;
        }
    }
    scanf("%d%d%d%d",&p[0].x,&p[0].y,&p[1].x,&p[1].y);
    mp[0][1]=mp[1][0]=get(p[0].x,p[0].y,p[1].x,p[1].y)*3/500;
    cnt=2;
    int xx,yy,flag=0;
    while(~scanf("%d%d",&xx,&yy))
    {
        if(xx==-1 && yy==-1)
        {
            flag=0;
            continue;
        }
        p[cnt].x=xx;
        p[cnt].y=yy;
        //如果这个站点是这条线路的起始站,就不能与前面的点按照40的速度生成边
        if(flag==0)
        {
            flag=1;
            cnt++;
            continue;
        }
        //加上min取最小值防止有相同点出现
        mp[cnt][cnt-1]=mp[cnt-1][cnt]=min(get(p[cnt-1].x,p[cnt-1].y,xx,yy)*3/2000,mp[cnt][cnt-1]);
        cnt++;
    }
    for(int i=0;i<cnt;i++)
        for(int j=i+1;j<cnt;j++)
            mp[i][j]=mp[j][i]=min(get(p[i].x,p[i].y,p[j].x,p[j].y)*3/500,mp[i][j]);
    dijkstra();
    printf("%.0f\n",dis[1]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值