POJ 2502 Subway (Shortest path)

Source: Internet
Author: User

Original title address: http://poj.org/problem?id=2502

Subway

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 7347

Accepted: 2387

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 youare lucky, and whenever your arrive at a subway station, a train is there tha Tyou can board immediately. You'll get on and off of the subway any number oftimes, and your may switch between different subway lines if you wish. Allsubway lines go in both directions.

Input

Input consists of the x, y coordinates of your home Andyour school, followed by specifications of several subway lines. Each subwayline consists of the non-negative integer x, y coordinates of each stop on theline, in order. Assume the subway runs in a straight line betweenadjacent 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 is followed bythe dummy coordinate pair-1,-1. In total there is at the most subway stops inthe city.

Output

Output is the number of minutes it would take you to Getto 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

21st

Test instructions

There is only one set of data, the first line is the coordinates of the home and the school coordinates, followed by the subway line the coordinates of each station, to ( -1,-1) end. It is known that the walking speed is 10km/h, the subway speed is 40km/h, and the shortest time (minutes, rounded to an integer) of the home to school.

Attention:

    1. The coordinate units given are meters, the units of the given speed are km/h, and the required result is minutes.
    2. Note that the time it takes to get from station 1th to station 3rd is the distance from station 1th to station 2nd divided by speed, plus the distance from station 2nd to station 3rd divided by speed. Instead of directly dividing the distance from station 1th to station 3rd, divide the speed. (It was because of this WA a few times (-.-!!))

#include <cstdio>#include<math.h>#include<algorithm>using namespacestd;Const intN = +, INF =0x3f3f3f3f;structpoint{intx; inty;} Vertex[n];DoubleTime[n][n] = {0};BOOLCnt[n] = {0};voidQwe (int&N) {    intx, y, start, end;  while(1) {Start=N;  for(; ; ++N) {if(!~SCANF ("%d%d", &x, &y))return ; if(x = =-1&& y = =-1) {End=N;  Break; }            Else{vertex[n].x=x; Vertex[n].y=y; }        }         for(inti=start+1; i<end; ++i) {DoubleDX = (vertex[i].x-vertex[i-1].x)/1000.0; DoubleDY = (vertex[i].y-vertex[i-1].Y)/1000.0; Time[i][i-1] = sqrt (dx*dx + dy*dy) *1.5; Time[i-1][i] = time[i][i-1]; }    }}intMainvoid) {scanf ("%d %d%d%d", &vertex[0].x, &vertex[0].Y, &vertex[1].x, &vertex[1].y); intn =2, Mintag;    Qwe (n);  for(intI=0; i<n; ++i) {Time[i][i]=INF;  for(intj=0; j<n; ++j) {if(Time[i][j] = =0)            {                DoubleDX = (vertex[i].x-vertex[j].x)/1000.0; DoubleDY = (VERTEX[I].Y-VERTEX[J].Y)/1000.0; TIME[I][J]= sqrt (dx*dx + dy*dy) *6; Time[j][i]=Time[i][j]; } }} cnt[0] =true;  for(intI=1; i<n; ++i) {Mintag=0;  for(intj=1; j<n; ++j)if(!cnt[j] && time[0][J] < time[0][mintag]) Mintag=J; Cnt[mintag]=true;  for(intj=1; j<n; ++j)if(!Cnt[j]) time[0][j] = min (time[0][J], time[0][mintag] +Time[mintag][j]); } printf ("%.0lf\n", time[0][1]);}

POJ 2502 Subway (Shortest path)

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.