Hncu 1598 find the most comfortable road (query set + enumeration)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1598

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 4 1 2 2 2 3 4 1 1 3 4 2 2 1 2

 

Sample output1 0 train of thought: first The route weight values are sorted in ascending order, and the lower limit of the weights are enumerated in sequence. Each time you enumerate a lower limit, the values are initialized once, and then KruskalAlgorithmUntil the two points A and B are connected, the extreme values of this road are recorded. Then, the next lower limit of the cyclic enumeration is greater than the previous lower limit. After the ownership value is enumerated, the minimum value in the extreme value is obtained.
Code:
 1 # Include <iostream> 2 # Include <algorithm> 3   Using   Namespace  STD;  4   # Define INF 9999999 5  Struct  Nod  6   {  7       Int  A, B, C;  8 } Node [ 1010  ];  9   Int Comp (nod A, nod B ){ Return A. c < B. C ;}  10   Int Parent [210  ];  11   Int Findp ( Int  A)  12   {  13       While (! = Parent [a])  14 A = Parent [a];  15       Return  A;  16  }  17   Void Merge ( Int A, Int  B)  18   {  19 A = Findp ();  20 B = Findp (B );  21       Int  T;  22      If (A> B)  23 T = A, A = B, B = T;  24       If (! = B)  25 Parent [B] = A;  26   }  27   Int  Main ()  28   { 29       Int  N, m, Q;  30       While (~ Scanf ( "  % D  " , & N ,& M ))  31   {  32           Int  I, J;  33           Int  A, B, C; 34           For (I = 1 ; I <= m; I ++ )  35 Scanf ( "  % D  " , & Node [I]. A, & node [I]. B ,& Node [I]. C );  36 Sort (node + 1 , Node + 1 + M, comp );  37 Scanf ("  % D  " ,& Q );  38           While (Q -- )  39   {  40 Scanf ( "  % D  " , & ,& B );  41               Int Min =INF;  42               For (I = 1 ; I <= m; I ++ )  43   {  44                   For (J = 1 ; J <= N; j ++ )  45 Parent [J] = J;  46                   For (J = I; j <= m; j ++)  47   {  48   Merge (node [J]. A, node [J]. B );  49                       Int X = Findp ();  50                       Int Y = Findp (B );  51                       If (X = Y)  52   { 53                           If (Min> node [J]. C- Node [I]. c)  54 Min = node [J]. C- Node [I]. C;  55                           Break  ;  56   }  57   }  58   }  59 Printf ("  % D \ n  " , Min = inf? - 1  : Min );  60   }  61   }  62       Return   0  ;  63 }

 

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.