HDU 1598 find the most comfortable road (query set | Shortest Path)

Source: Internet
Author: User
Find the most comfortable road

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3720 accepted submission (s): 1583


Problem descriptionxx has many cities that communicate with each other through a strange highway SARS (super air roam structure --- super Air Roaming structure, each piece of SARS imposes a fixed speed limit on the flycar driving. Meanwhile, XX has special requirements on flycar's "comfort, that is, the lower the speed difference between the highest speed and the lowest speed, the more comfortable the ride. (It is understood that flycar must speed up/speed down instantly, which is painful ),
However, XX has less requirements on time. You need to find the most comfortable path between cities. (SARS is bidirectional ).

 

Input includes multiple test instances, each of which includes:
The first line has two positive integers n (1 <n <= 200) and M (M <= 1000), indicating that there are n cities and m sars.
The following rows are three positive integers, startcity, endcity, and speed. startcity is converted to endcity on the surface, and the speed limit is speedsars. Speed <= 1000000
Then there is a positive integer Q (q <11), indicating the number of pathfinding.
Next, each row in row Q has two positive integers start and end, indicating the start and end of the path.

 

Output: print a line for each route seeking. Only one non-negative integer is output, indicating the maximum comfort speed and the lowest speed of the optimal route. If the start and end cannot be reached, the output is-1.

 

Sample input4 41 2 22 3 41 4 13 4 22 1 31 2

 

Sample output10

 

Authorailyanlu

 

Sourcehdu 2007-spring Programming Contest-warm up (1)

 

Recommend8600

 

Check the set

Sort each edge by speed in ascending order, and then enumerate each edge from small to large until the start and end are in the same set, and then update the maximum value-minimum value of speed.

1 # include <cstdio> 2 # include <cstring> 3 # include <stdlib. h> 4 # include <algorithm> 5 using namespace STD; 6 const int maxn = 1000 + 10; 7 const int INF = 0x3f3f3f; 8 stru9 CT node10 {11 int X, Y; 12 INT val; 13 bool operator <(const node & B) const14 {15 return Val <B. val; 16} 17} A [maxn]; 18 int P [maxn]; 19 20 int find (int x) 21 {22 return P [x] = x? X: P [x] = find (P [x]); 23} 24 25 int main () 26 {27 // freopen ("in.txt", "r ", stdin); 28 int n, m; 29 While (scanf ("% d", & N, & M )! = EOF) 30 {31 for (INT I = 1; I <= m; I ++) 32 scanf ("% d", & A [I]. x, & A [I]. y, & A [I]. val); 33 sort (a + 1, A + m + 1); // sort 34 int Kase, Star, en, Minn in ascending order of speed; 35 scanf ("% d", & Kase); 36 while (Kase --) 37 {38 scanf ("% d", & star, & en ); 39 Minn = inf; // The initialization of Minn should be put out. Wa should be put in at first, because if Minn is put in it, 40 for (INT I = 1; I <= m; I ++) // update the minimum value 41 {42 for (int K = 1; k <= m; k ++) 43 p [k] = K; 44 For (Int J = I; j <= m; j ++) // find the maximum 45 {46 int x = find (A [J]. x); 47 Int y = find (A [J]. Y); 48 if (X! = Y) 49 p [x] = y; 50 if (find (STAR) = find (en) 51 {52 Minn = min (Minn, a [J]. val-A [I]. val); // because the order is arranged, it must be a [I]. val> = A [I]. val53 break; 54} 55} 56 If (Minn = inf) // If the Minn cannot be updated for the first time, the starting and ending points cannot be connected to 57 break; 58} 59 If (Minn = inf) 60 printf ("-1 \ n"); 61 else62 printf ("% d \ n", Minn ); 63} 64} 65 return 0; 66}
View code

Best Practice

 

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.