POJ 2502 Subway (Dijkstra)

Source: Internet
Author: User

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6692 Accepted: 2177

Description

You had 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 being late for class, and you want to know how long it'll take your to get to school.
You walk at a speed of ten km/h. The subway travels at +/-assume that's lucky, and whenever you arrive at a subway station, a train is there th At you can board immediately. You'll get on and off the subway any number of times, and your 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. Assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each of the line have at least and stops. The end of each subway line was followed by the dummy coordinate pair-1,-1. In total there is at most of subway stops in the city.

Output

Output is the number of minutes it would take you to get to school, rounded to the nearest minute, taking the fastest route .

Sample Input

0 0 10000 10000 200 5000 200 7000 200-1-1 2000 600 5000 600 10000 600-1-1

Sample Output

21st

Title Link: http://poj.org/problem?id=2502


Title: To the monk and school coordinates to express a fixed starting point and end point, next each line represents a subway line, enter the subway line of each site coordinates, to -1,-1 (not coordinates) end line, to ensure that the subway line along a straight line, and at least two stations. Give Chuyang subway and walk at different speeds, ask a person from home to school to use the minimum time.


Problem-Solving ideas: The construction of the graph, time for the weight, with the Dijkstra algorithm, the home to the school two nodes shortest path problems. The time between two stations on the same subway line is calculated first, the input ends, and no weights are calculated at any two of the walking speed. Note the input format, ending with EOF.

add: Dijkstra algorithm applies to the graph structure with positive weights, dist[i] array stores the shortest path length of I to the starting point V0, and the initial adjacency matrix Eg[v0][i] Value infinity. Traverse n-1 times to find the N-1 strip the shortest. S[i] Array records whether the node has determined the minimum dist[i], the initial is 0, the determination is 1, the found I value is assigned u, start with u to find the next distance u nearest node J, update Condition: dist [j] = min (dist [u]+eg [V0] [u],dist [ J]) to ensure the shortest distance from the starting point. If you want to log the path, use the path array, if you find J,Path [j]=u record.


The code is as follows:

#include <cstdio> #include <cstring> #include <cmath> #define INF 100000000.0;int const Maxn=400;int s[ MAXN];d ouble DIST[MAXN];d ouble eg[maxn][maxn];int num;struct a{double x, y;}    STOP[MAXN];d ouble lenth (double x1,double y1,double x2,double y2) {double a= (x1-x2) * (X1-X2) + (y1-y2) * (Y1-Y2); return sqrt (a);}    void Dijkstra (int v0) {int i,j;        for (i=0;i<num;i++) {s[i]=0;    Dist[i]=eg[v0][i];    } s[v0]=1;        for (i=0;i<num-1;i++) {double min=inf;        int u;                for (j=0;j<num;j++) {if (!s[j] && dist[j]<min) {u=j;            MIN=DIST[J];        }} s[u]=1; for (j=0;j<num;j++) {if (!s[j] && dist[u]+eg[u][j]<dist[j]) dist[j]=dist[u]+        EG[U][J];    }}}int Main () {int i,j;    Double x1,x2,y1,y2;    memset (eg,0,sizeof (eg)); scanf ("%lf%lf%lf%lf", &x1,&y1,&x2,&y2); stop[0].x=x1; stop[0].y=y1;stop[1].x=x2;stop[1].y=y2;    num=2;    int p=0;        while (scanf ("%lf%lf", &stop[num].x,&stop[num].y)!=eof) {if (stop[num].x==-1 && stop[num].y==-1)            {p=0;        Continue            } if (!p) {p=1;            num++;        Continue        } eg[num-1][num]=eg[num][num-1]=lenth (STOP[NUM-1].X,STOP[NUM-1].Y,STOP[NUM].X,STOP[NUM].Y)/4000.0;    num++; } for (i=0;i<num;i++) {for (j=i+1;j<num;j++) if (eg[i][j]==0) eg[i][j]=eg[j][i]=    Lenth (STOP[I].X,STOP[I].Y,STOP[J].X,STOP[J].Y)/1000.0; }dijkstra (0);p rintf ("%d\n", int (0.5+6.0*dist[1])); return 0;}


POJ 2502 Subway (Dijkstra)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.