By the underground or by Foot? Time limit:1.0 Second
Memory limit:64 Mbimagine yourself in a big city. You want to get from point
Ato point
B. To-do-you-may-move by foot or use the underground. Moving by the underground are faster but you may enter and exit it only at the stations. To save your time decided to write a program to find the fastest route. Inputthe first line contains, floating point numbers. First of them is the speed of traveling by foot. The second one is the speed of traveling by the underground. The second speed are always greater than the first one. Then description of the underground follows. It starts with an integer number
NIn the first line. It is the number of the underground stations. Assume that
Nis not greater than 200. The following
NLines contain, floating point numbers each (
I-th line contains the coordinates of
I-th station). Then the description of the connections between stations follows. Each connection was determined by the pair of integers, i.e. by the numbers of connected stations. The list of connections is terminates with a pair of zeroes. We assume that all the connections is straight. So the time we need to travel between stations are equal to the distance between stations divided by the speed of traveling By the underground. It should is mentioned also that entering and exiting the underground and changing trains is possible at the stations ONL Y and takes no time. At the coordinates of the points
Aand
Bis given, tha pair of coordinates in a line. Outputthe first line should contain the minimal time needed to get from the point
ATo the point
B. Time should is given with the precision of 10−6. The second line describes the use of the underground while traveling. It starts with the number of visited stations with tha following list of visited stations in the order they should is Visi Ted. Sample
input |
Output |
1 10040 01 09 09 91 21 32 40 010 1010 0 |
2.63462954 4 2) 1 3 |
problem Author:Alexander klepinin "Analysis" shortest way, SPFA.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 0x3f3f3f3f#defineMoD 10000typedefLong Longll;using namespacestd;Const intn=205;Const intm=100005;intn,m,k=0;Doubleb;intVis[n],pre[n];DoubleW[n][n],d[n];Charstr[ -];BOOLflag=false;structman{Doublex, y;} Point[n];voidSPFA () {d[0]=0; vis[0]=1; Queue<int>Q; Q.push (0); while(!Q.empty ()) { intt=Q.front (); Q.pop (); Vis[t]=0; for(intI=0; i<=n+1; i++){ if(d[i]>d[t]+W[t][i]) {D[i]=d[t]+W[t][i]; Pre[i]=T; if(!vis[i]) Q.push (i), vis[i]=1; } } }}intMain () { for(intI=0; i<n;i++) {D[i]=10000000; for(intj=0; j<n;j++) {W[i][j]=10000000; }} scanf ("%LF%LF",&a,&b); scanf ("%d",&N); intu,v; for(intI=1; i<=n;i++) {scanf ("%LF%LF",&point[i].x,&point[i].y); } while(~SCANF ("%d%d", &u,&v) &&u&&v) { DoubleS=sqrt ((point[u].x-point[v].x) * (point[u].x-point[v].x) + (POINT[U].Y-POINT[V].Y) * (point[u].y-point[v].y)); W[U][V]=w[v][u]=s/b; } scanf ("%LF%LF%LF%LF", &point[0].x,&point[0].y,&point[n+1].x,&point[n+1].y); for(intI=0; i<=n+1; i++){ for(intj=i+1; j<=n+1; j + +){ if(w[i][j]<10000000)Continue; DoubleS=sqrt ((point[i].x-point[j].x) * (point[i].x-point[j].x) + (POINT[I].Y-POINT[J].Y) * (point[i].y-point[j].y)); W[I][J]=w[i][j]=s/A; }} SPFA (); printf ("%.7lf\n", d[n+1]); Stack<int>p; for(inti=n+1;p re[i];i=Pre[i]) {P.push (pre[i]); } printf ("%d", P.size ()); while(!p.empty ()) printf ("%d", P.top ()), P.pop (); return 0;}
View Code
URAL 1205 by the underground or by Foot? (SPFA)