POJ 2502 - Subway(单源最短路)

来源:互联网 发布:淘宝一键复制有坏处吗 编辑:程序博客网 时间:2024/05/22 03:33

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9121 Accepted: 2959
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

题意:
给出起点和终点的坐标,还有若干个地铁线。
每个地铁线至少有2个站点,可以步行也可以搭乘地铁。
给出了步行的速度和地铁的速度,问从起点到终点的最短时间。

解题思路:
此题的难点是构造图。
开始对于每条地铁,都记录下相邻的点的时间。
然后对于任意两点都用步行去更新,最后用dijkstra扫一遍即可。

POJ的G++对double类型不友好,要用C++。

AC代码:

#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 3e2+5;const double INF = 0x3f3f3f3;const double v1=10000.0/60;const double v2=40000.0/60;struct node {double x,y;}point[maxn];double mp[maxn][maxn];double dis[maxn];bool vis[maxn];double distan(node a,node b)    {return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void dijkstra(int n){    for(int i = 1;i <= n;i++)   dis[i] = INF,vis[i] = 0;    dis[1] = 0;    for(int i = 1;i <= n;i++)    {        int index = -1;double minn = INF;        for(int j = 1;j <= n;j++)            if(!vis[j]&&dis[j] < minn)  minn = dis[j],index = j;        vis[index] = 1;        for(int j = 1;j <= n;j++)            if(!vis[j] && dis[j] > dis[index]+mp[index][j])                dis[j] = dis[index] + mp[index][j];    }    printf("%.0lf\n",dis[2]);}int main(){    for(int i = 1;i < maxn;i++)        for(int j = 1;j < maxn;j++)            mp[i][j] = i==j?0:INF;    scanf("%lf%lf%lf%lf",&point[1].x,&point[1].y,&point[2].x,&point[2].y);    int n = 2;    int cnt = 3;    double a,b;    while(~scanf("%lf%lf",&a,&b))    {        if(a==-1 && b==-1)        {            cnt = n+1;            continue;        }        n++;        point[n].x = a,point[n].y = b;        if(n != cnt)    mp[n][n-1] = mp[n-1][n] = min(mp[n][n-1],distan(point[n],point[n-1])/v2);    }    for(int i = 1;i <= n;i++)        for(int j = 1;j <= n;j++)            mp[i][j] = mp[j][i] = min(mp[i][j],distan(point[i],point[j])/v1);    dijkstra(n);    return 0;}
0 0
原创粉丝点击