Easter holidays Time Limit: 1 second memory limit: 32768 kb Special Judge
Scandinavians often make vacation during the Easter holidays in the largest ski resort are. are provides fantastic ski conditions, equalski lifts and slopes of various difficulty profiles. however, some lifts go faster than others, and some are so popular that a queue forms at the bottom.
Per is a beginner skier and he is afraid of lifts, even though he wants to ski as much as possible. now he sees that he can take several different lifts and then different slopes or some other lifts, and this freedom of choice is starting to be too puzzling...
He wowould like to make a ski journey that:
- Starts at the bottom of some lift and ends at that same spot
- Has only two phases: in the first phase, he takes one or more lifts up, in the second phase, he will ski all the way down back to where he started
- Is least scary, that is the ratio of the time spent on the slopes to the time spent on the lifts or waiting for the lifts is the largest possible.
Can you help per find the least scary ski journey? A ski resort contains N places, M slopes, and K lifts (2 <= n <= 1000, 1 <= m <= 1000, 1 <= k <= 1000 ). the slopes and lifts always lead from some place to another place: the slopes lead from places with higher altitude to places with lower altitude and lifts vice versa (lifts cannot be taken downwards ).
Input
The first line of the input contains the number of cases-the number of ski resorts to process. each ski resort is described as follows: the first line contains three integers n, m, and K. the following M lines describe the slopes: each line contains three integers-top and bottom place of the slope (the places are numbered 1 to n ), and the time it takes to go down the slope (max. 10000 ). the final K lines describe the lifts by three integers-the bottom and top place of the lift, and the time it takes to wait for the lift in the queue and be brought to its top station (max. 10000 ). you can assume that no two places are connected by more than one lift or by more than one slope.
Output
For each input case, the program shocould print two lines. the first line shoshould contain a space-separated list of places in the order they will be visited-the first place shoshould be the same as the last place. the second line shoshould contain the ratio of the time spent in the slopes to the time spent on the lifts or wating for the lifts. the ratio shoshould be rounded to the closest 1/1000th. if there are two possibilities, then the rounding is away from zero (e.g ., 1.9812 and 1.9806 become 1.981, 3.1335 becomes 3.134, and 3.1345 becomes 3.135 ). if there are multiple journeys that prior to rounding are equally scary, print an arbitrary one.
Sample Input
15 4 31 3 122 3 63 4 95 4 94 5 125 1 124 2 18
Sample output
4 5 1 3 40.875
Question: The maximum ratio of the elevator time to the skiing time. Use spfa to find the time at each point, mainly because the output path is difficult.
# Include "stdio. H "# include" string. H "# include" vector "# include" queue "# include" iostream "# include" algorithm "using namespace STD; # define n 1005 const int INF = (INT) 1e10; struct node {int V, W, next;} G1 [N], G2 [N]; int T1, T2, dis1 [N] [N], dis2 [N] [N]; int head1 [N], head2 [N], pre1 [N] [N], pre2 [N] [N]; void Add1 (int u, int V, int W) // pre array record path {G1 [T1]. V = V; G1 [T1]. W = W; G1 [T1]. next = head1 [u]; head1 [u] = T1 ++;} void Add2 (int u, int v, Int W) {G2 [T2]. V = V; G2 [T2]. W = W; G2 [T2]. next = head2 [u]; head2 [u] = t2 ++;} void spfa1 (int u) {int I, S, V, Mark [N]; memset (mark, 0, sizeof (Mark); Mark [u] = 1; S = u; queue <int> q; q. push (s); dis1 [u] [u] = 0; while (! Q. Empty () {u = Q. Front (); q. Pop (); Mark [u] = 0; for (I = head1 [u]; I! =-1; I = G1 [I]. Next) {v = G1 [I]. V; If (dis1 [s] [u]! =-1 & dis1 [s] [v] <dis1 [s] [u] + G1 [I]. w) {dis1 [s] [v] = dis1 [s] [u] + G1 [I]. w; pre1 [s] [v] = u; If (! Mark [v]) {q. push (V); Mark [v] = 1 ;}}}} void spfa2 (int u) {int I, S, V, Mark [N]; memset (mark, 0, sizeof (Mark); Mark [u] = 1; S = u; queue <int> q; q. push (s); dis2 [u] [u] = 0; while (! Q. Empty () {u = Q. Front (); q. Pop (); Mark [u] = 0; for (I = head2 [u]; I! =-1; I = G2 [I]. Next) {v = G2 [I]. V; If (dis2 [s] [u]! = Inf & dis2 [s] [v]> dis2 [s] [u] + G2 [I]. w) {dis2 [s] [v] = dis2 [s] [u] + G2 [I]. w; pre2 [s] [v] = u; If (! Mark [v]) {q. push (V); Mark [v] = 1 ;}}}}/* void path (int s, int T, int pre [N] [N]) {If (S = T) return; Path (pre [T] [s], T, pre); printf ("% d", S );} */INT main () {int t, n, m, K, U, V, W, I, j; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & M, & K); memset (head1,-1, sizeof (head1 )); memset (head2,-1, sizeof (head2); T1 = t2 = 0; for (I = 0; I <m; I ++) {scanf ("% d", & U, & V, & W); Add1 (U, V, W) ;}for (I = 0; I <K; I ++) {scanf ("% d", & U, & V, & W); Add2 (U, V, W);} memset (pre1,-1, sizeof (pre1); memset (pre2,-1, sizeof (pre2); memset (dis1,-1, sizeof (dis1); for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) dis2 [I] [J] = inf;} for (I = 1; I <= N; I ++) {spfa1 (I); // calculate the maximum value spfa2 (I); // calculate the minimum value} double ans = 0; int S, T; for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {if (I = j | dis2 [I] [J]> = inf | dis1 [J] [I] =-1) continue; double TMP = 1.0 * dis1 [J] [I]/dis2 [I] [J]; If (TMP> ans) {ans = TMP; S = I; t = J ;}} printf ("% d", S); I = s; j = T; k = 0; int A [n]; // record the answer path while (pre2 [I] [J]! = S) // find the path from the end point to the start point {// end to the start point A [k ++] = pre2 [I] [J]; j = pre2 [I] [J];} for (I = K-1; I> = 0; I --) printf ("% d", a [I]); printf ("% d", T); I = T; j = s; k = 0; while (pre1 [I] [J]! = T) {A [k ++] = pre1 [I] [J]; j = pre1 [I] [J];} for (I = K-1; i> = 0; I --) printf ("% d", a [I]); printf ("% d \ n", S); // path (t, s, pre2); // path (S, T, pre1); printf ("%. 3f \ n ", ANS);} return 0 ;}
Zoj 3088 Easter holidays (spfa)