【最短路】POJ2502 SUBWAY (spfa)

subway
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


题意:给你起点和终点,下面每一行是一条地铁线,每两个相邻整数代表一个地铁站的坐标;地铁站之间只能相邻站点行驶;地铁40km/h,步行10km/h;题中的坐标是以为单位的;

思路:从起点1到终点k的最短路;都清楚题意就可以了,我的错误在于没有注意地铁站之间只能相邻站点行驶的条件;

代码:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
using namespace std;
const int maxn=205;
const double inf=0x3f3f3f3f;
int k,l;

struct node
{
    double x,y;
    int num;
} s[maxn];

bool vis[maxn];
double dis[maxn];

void spfa(int st)
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<maxn;i++)
        dis[i]=inf;

    vis[st]=true;
    dis[st]=0;

    queue<int > que;
    while(!que.empty())  que.pop();

    que.push(st);
    while(!que.empty())
    {
        int u=que.front();
        vis[u]=false;
        que.pop();

        for(int i=1; i<=k; i++)
        {
            if(i==u)  continue;

            double far,t;
            far=sqrt(((s[u].y-s[i].y)*(s[u].y-s[i].y))+((s[u].x-s[i].x)*(s[u].x-s[i].x)));
            far=far/1000.0;
            if(fabs(s[u].num-s[i].num)==1)
                t=far/40.0;
            else
                t=far/10.0;

            if(dis[i]>dis[u]+t)
            {
                dis[i]=dis[u]+t;
                if(!vis[i])
                {
                    vis[i]=true;
                    que.push(i);
                }
            }
        }
    }
}

int main()
{
    double s1,s2,e1,e2;
    double x,y;
    k=2,l=1;

    scanf("%lf%lf%lf%lf",&s1,&s2,&e1,&e2);
    while(~scanf("%lf%lf",&x,&y))
    {
        if(x==-1&&y==-1)
        {
            if(l==8)
                break;
            l++;
            continue;
        }

        s[k].x=x,s[k].y=y;
        s[k++].num=l++;
    }

    s[1].x=s1,s[1].y=s2,s[1].num=-1;
    s[k].x=e1,s[k].y=e2,s[k].num=-3;
    spfa(1);

    printf("%d\n",(int)(dis[k]*60+0.5));
//    printf("%.0f\n",floor(dis[k]*60+0.5));
    return 0;
}
以上两种四舍五入为整数的输出方式均可,注意第二种输出方式只等用%f!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值