L - Subway POJ - 2502

L - 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

题意:一个人从家要到学校去,途中有许多车站,所以有步行和做地铁两种方式,其速度分别是10km/h 和40km/h。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?

题解:建图十分麻烦,要把车站之间的车行时间算出来,还有任意两点间的步行距离算出来,然后以时间为边权建图,再跑一遍最短路。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <math.h>
using namespace std;
const double inf = 0x3f3f3f3f;
const int maxn = 100100;
const double walk = 10000 / 60;//转换单位为m/s
const double car = 40000 / 60;//转换单位为m/s
int head[maxn],inq[maxn],tot,cnt;
double dis[maxn];

struct node
{
    double x;
    double y;
}nod[maxn];

struct edge
{
    int v;
    int next;
    double w;
}edg[maxn];

double len(double x1,double y1,double x2,double y2)//求距离
{
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

void addnode(int u,int v,double w)
{
    edg[tot].v = v;
    edg[tot].w = w;
    edg[tot].next = head[u];
    head[u] = tot++;
}

void SPFA()
{
    for(int i = 1;i <= cnt;i++) dis[i] = 100000000;
    memset(inq,0,sizeof(inq));

    queue<int>Q;
    Q.push(1);
    inq[1] = 1;
    dis[1] = 0;

    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        inq[u] = 0;
        for(int i = head[u];i != -1;i = edg[i].next)
        {
            int v = edg[i].v;
            double w = edg[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!inq[v])
                {
                    Q.push(v);
                    inq[v] = 1;
                }
            }
        }
    }
}

int main()
{
    scanf("%lf %lf %lf %lf",&nod[1].x,&nod[1].y,&nod[2].x,&nod[2].y);

    cnt = 3;
    tot = 0;
    int flag = 0;//flag 表示是否时一个站的第一位
    memset(head,-1,sizeof(head));

    while(scanf("%lf %lf",&nod[cnt].x,&nod[cnt].y) != EOF)//输入站点并计算在同一线路上相邻车站之间的车行时间
    {
        if(nod[cnt].x != -1 && nod[cnt].y != -1)
        {
            if(!flag) flag = 1;
            else
            {
                double w = len(nod[cnt].x,nod[cnt].y,nod[cnt - 1].x,nod[cnt - 1].y);
                w /= car;
                addnode(cnt,cnt - 1,w);
                addnode(cnt - 1,cnt,w);
            }
            cnt++;
        }
        else
            flag = 0;
    }

    for(int i = 1;i <= cnt;i++)//记录各点之间的步行时间
    {
        for(int j = i + 1;j <= cnt;j++)
        {
            double w = len(nod[i].x,nod[i].y,nod[j].x,nod[j].y);
            w /= walk;
            addnode(i,j,w);
            addnode(j,i,w);
        }
    }

    SPFA();

    printf("%.0f\n",dis[2]);

    return 0;

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值