Subway
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6689 |
|
Accepted: 2176 |
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'll take-to-get to school, rounded to the nearest minute, taking the fastest R Oute.
Sample Input
0 0 10000 10000 200 5000 200 7000 200-1-1 2000 600 5000 600 10000 600-1-1
Sample Output
21st
Source
Waterloo Local 2001.09.22
Title Link: http://poj.org/problem?id=2502
The first line gives the start and end coordinates, and then each line is a subway line, with a coordinate representation, to 1-1 means that the line input is complete, note unit is meters! Each line is straight two-way, the subway speed 40km/h, people walking speed 10km/h, the subway can only be exercised between the adjacent two stations, can not directly from the station I to I+2 station, if the person to the subway station there is a subway sit, ask from the beginning to the end of the minimum need a few minutes
Problem analysis: The input of this problem is more troublesome, each subway line we have to deal with alone, Descartes distance/Metro speed (40km/h) as a side right, processing each line, and then processing other points between the Benquan, Descartes Distance/human speed (10km/h), and then the bare shortest problem, With Dijkstra solution, pay attention to 3 questions, 1th: unit of conversion, 2nd: The result requires rounding, section 3: Infinity set to double type!
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm>using namespace std; int const MAX = 405;int Const INF = 100000000.0;struct node{double u, V;} Nd[max];d ouble Dis[max], E[max][max];bool vis[max];int cnt;double Get_dis (double x1, double y1, double x2, double y2) { return sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2));} void Dijkstra (int v0) {for (int i = 0; i < cnt; i++) dis[i] = E[v0][i]; Dis[v0] = 0; Vis[v0] = true; for (int i = 0; i < cnt-1; i++) {Double mi = INF; int u = v0; for (int j = 0; J < CNT; J + +) {if (!vis[j] && mi > dis[j]) {u = J mi = dis[j]; }} Vis[u] = true; for (int k = 0; k < cnt; k++) if (!vis[k] && dis[k] > Dis[u] + e[u][k]) dis[k] = dis [u] + e[u][k]; }}int Main () {memset (Vis, false, sizeof (VIS)); memset (E, 0, SizEOF (e)); scanf ("%lf%lf%lf%lf", &nd[0].u, &ND[0].V, &nd[1].u, &ND[1].V); Double U, v; int tmp = 2; CNT = 2; while (scanf ("%lf%lf", &u, &v)! = EOF) {if (U = = -1.0 && v = = -1.0) {for (int i = tmp; i < cnt-1; i++) {Double get = Get_dis (nd[i].u, ND[I].V, Nd[i + 1].u, Nd[i + 1].v)/40000.0; E[i][i + 1] = E[i + 1][i] = get; } TMP = CNT; Continue } nd[cnt].u = u; ND[CNT++].V = v; } for (int i = 0, i < CNT; i++) for (int j = i + 1; j < CNT; J + +) if (e[i][j] = = 0) E[I][J] = E[j][i] = Get_dis (nd[i].u, ND[I].V, nd[j].u, ND[J].V)/10000.0; Dijkstra (0); printf ("%d\n", (int) (dis[1] * 60.0 + 0.5));}
POJ 2502 Subway (Dijkstra shortest circuit + build)