1. 程式人生 > >POJ 2502 Subway(最短路)

POJ 2502 Subway(最短路)

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6229 Accepted: 2026

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

Source

          題目大意:一個人從家要到學校去,途中有許多車站,所以有步行和做地鐵兩種方式,其速度分別是10km/h 和40km/h。輸入的規則是第一行輸入的是x1,y1,x2,y2,分別代表家的座標和學校的座標。以後輸入的是車站的座標,數目不超過200,相鄰的兩個站點可以坐地鐵,其他的需要步行。問到達學校的最短時間是多少?
         因為不知道輸入的資料有多少,所以用while(scanf()!=EOF)。其他的就沒有什麼要注意的了,建圖很重要。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>

using namespace std;
const int N = 300;
const int INF = 9999999;

double map[N][N];
double num[N];
int v[N];
int n,m;
structnode
{
    int x,y;
}q[10001];
void dijkstra()
{
    for(int i=0;i<n;i++)
    {
        num[i] = map[0][i];
        v[i] = 0;
    }
    num[0] = 0;
    for(int i=0;i<n;i++)
    {
        int min = INF,k;
        for(int j=0;j<n;j++)
        {
            if(v[j] == 0 && num[j]<min)
            {
                min = num[j];
                k = j;
            }
        }
        if(min == INF)
        {
            break;
        }
        v[k] = 1;
        for(int j=0;j<n;j++)
        {
            if(v[j] == 0 && num[j] > num[k] + map[k][j])
            {
                num[j] = num[k] + map[j][k];
            }
        }
    }
    printf("%.0lf\n",num[1]);
}

int main()
{
    while(cin >> q[0].x >> q[0].y >> q[1].x >> q[1].y)
    {
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<=N;j++)
            {
                map[i][j] = INF;
            }
            map[i][i] = 0;
        }
        n = m = 2;
        while(cin >> q[n].x >> q[n].y)
        {
            if(q[n].x == -1 && q[n].y == -1)
            {
                m = n;
                continue;
            }
            if(n!=m)
            {
                double d = sqrt((double)(q[n].y-q[n-1].y)*(q[n].y - q[n-1].y) + (q[n].x - q[n-1].x)*(q[n].x - q[n-1].x));
                d = 3*d/2000;
                map[n][n-1] = d;
                map[n-1][n] = d;
            }
            n++;
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(map[i][j] == INF)
                {
                    double d = sqrt((double)(q[j].y-q[i].y)*(q[j].y - q[i].y) + (q[j].x - q[i].x)*(q[j].x - q[i].x));
                    d = 3*d/500;
                    map[i][j] = d;
                    map[j][i] = d;
                }
            }
        }
        dijkstra();
    }
    return 0;
}