Zoj 3088 Easter holidays (spfa calculates the longest path, the shortest path, and the print path)

Source: Internet
Author: User

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
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
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
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
Source:Norgesmesterskapet I programmering, 2004
Submit status


Question:

There are two sides to a ski resort (one side can be seen as a sled rising side, and one side can be seen as a side from a snowy slope). Let you find two points A and B, a-> B goes up through the first edge. The time required is T1, B-> A goes down through the second edge, and the time required is t2. This maximizes T2/T1.


Ideas:

Create two graphs with two edges, and use spfa n times to find the shortest path from any point to any point in an image, use spfa n times to find the longest path from any point to any point in another graph, and then obtain the maximum value of T2/T1 through enumeration.


Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define maxn 1005 using namespace STD; const int INF = 0x3f3f3f; int n, m, CNT, Sx, cxx; double ans; bool vis [maxn]; int P [maxn]; int dist1 [maxn] [maxn], dist2 [maxn] [maxn]; int path1 [maxn] [maxn], path2 [maxn] [maxn]; struct node {int R, cost; int next;} edge1 [maxn], edge2 [maxn]; queue <int> q; void Init () {memset (p, 0, sizeof (p); memset (path1, 0, sizeof (Pat H1); memset (path2, 0, sizeof (path2); memset (dist1, 0, sizeof (dist1); memset (dist2, 0x3f, sizeof (dist2);} void addedge1 (int u, int V, int W) // create the first graph {CNT ++; edge1 [CNT]. R = V; edge1 [CNT]. cost = W; edge1 [CNT]. next = P [u]; P [u] = CNT;} void addedge2 (int u, int V, int W) // create the second figure {CNT ++; edge2 [CNT]. R = V; edge2 [CNT]. cost = W; edge2 [CNT]. next = P [u]; P [u] = CNT;} void spfa1 (int K) // find the longest path to initialize the smallest met large update {int I, j, nx; memset (VIS, 0, sizeof (VIS); SX = K; while (! Q. empty () Q. pop (); path1 [SX] [SX] = SX; dist1 [SX] [SX] = 0; vis [SX] = 1; q. push (SX); While (! Q. empty () {Nx = Q. front (); vis [NX] = 0; q. pop (); for (I = P [NX]; I; I = edge1 [I]. next) {If (dist1 [SX] [edge1 [I]. r] <dist1 [SX] [NX] + edge1 [I]. cost) {dist1 [SX] [edge1 [I]. r] = dist1 [SX] [NX] + edge1 [I]. cost; path1 [SX] [edge1 [I]. r] = NX; If (! Vis [edge1 [I]. r]) {vis [edge1 [I]. r] = 1; q. push (edge1 [I]. r) ;}}}} void spfa2 (int K) // find the shortest path. initialize the shortest path. The maximum update value is: {int I, j, nx; memset (VIS, 0, sizeof (VIS); SX = K; while (! Q. empty () Q. pop (); path2 [SX] [SX] = SX; dist2 [SX] [SX] = 0; vis [SX] = 1; q. push (SX); While (! Q. empty () {Nx = Q. front (); vis [NX] = 0; q. pop (); for (I = P [NX]; I; I = edge2 [I]. next) {If (dist2 [SX] [edge2 [I]. r]> dist2 [SX] [NX] + edge2 [I]. cost) {dist2 [SX] [edge2 [I]. r] = dist2 [SX] [NX] + edge2 [I]. cost; path2 [SX] [edge2 [I]. r] = NX; If (! Vis [edge2 [I]. r]) {vis [edge2 [I]. r] = 1; q. push (edge2 [I]. r) ;}}}} void output1 (int e, int s) // print Path 1 {If (E = s) return; else {output1 (path1 [s] [e], S); printf ("% d", e) ;}} void output2 (int e, int S) // print Path 2 {If (E = s) printf ("% d", e); else {output2 (path2 [s] [e], S ); printf ("% d", e) ;}int main () {int I, j, T, U, V, W, S, E; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M, & cxx ); init (); CNT = 0; for (I = 1; I <= m; I ++) {scanf ("% d", & U, & V, & W); addedge1 (U, V, W) ;}for (I = 1; I <= N; I ++) {spfa1 (I );} CNT = 0; memset (p, 0, sizeof (p); for (I = 1; I <= cxx; I ++) {scanf ("% d", & U, & V, & W); addedge2 (U, V, W) ;}for (I = 1; I <= N; I ++) {spfa2 (I);} ans = 0; for (I = 1; I <= N; I ++) // update ans {for (j = 1; j <= N; j ++) {if (I = j | dist2 [I] [J] = inf) continue; If (dist1 [J] [I] * 1.0/dist2 [I] [J]> ans) {S = I, E = J; ans = dist1 [J] [I] * 1.0/dist2 [I] [J] ;}} output2 (E, S); output1 (S, e ); printf ("\ n %. 3f \ n ", ANS);} return 0 ;}


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.