1. 程式人生 > >Subway(單源最短路)

Subway(單源最短路)

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;
}