HDU 1598 find the most comfortable road (query set + Brute Force Search)

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): 1662 accepted submission (s): 683

Problem descriptionxx
There are many cities in the city, and the city passes through a strange highway SARS (super air roam
Structure --- super Air Roaming structure), each SARS restricts a fixed speed for the flycar driving on, and XX star
Flycar has special requirements for "comfort", that is, the lower the difference between the highest speed and the lowest speed, the more comfortable the ride.
, (To understand the speed limit requirement of SARS, 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 221 31 2

 

Sample output10 analysis: (1) when I first saw this question, I first thought of the search, so I used the deep search first. This is my deep search. Code , But the timeout is not enough. As for whether it is correct and cannot be verified, I think it is still available, but it cannot be passed (⊙ o ⊙ )... The Code of Deep Search (time-out), if you have already used deep search, please advise, do you have other good pruning ideas ???? View code

# Include <stdio. h> # Include < String . H>//  # Define max_speed-1  //  # Define min_speed 1000010  Int  Max_speed, min_speed, min;  Int  N, m, Q;  Int  Begin, end;  Int Speed [ 210 ] [ 210  ];  Int Mark [ 210 ];  //  Pos pre  Void DFS ( Int POs, Int Maxs, Int  Mins ){  Int  I;  For (I = 1 ; I <= N; I ++ ){  If (MARK [I] = 1 )Continue ; //  Accessed          If (I = POS) Continue ; //  Yourself          If (Speed [POS] [I] =- 1 ) Continue ; //  Not directly          If (Speed [POS] [I] < Mins) mins =Speed [POS] [I];  If (Speed [POS] [I]> Maxs) Maxs = Speed [POS] [I];  If (I = End ){  If (Maxs-mins < Min) min = Maxs- Mins;  Return  ;}  If (Min = 0 )Return ; //  If the minimum value is found, return Mark [I] = 1  ; DFS (I, maxs, mins); Mark [I] = 0  ;}}  Int  Main (){  Int  X, Y, SD;  While (Scanf ( "  % D  " , & N, & M )! =EOF) {memset (speed, - 1 , Sizeof  (Speed); memset (mark,  0 , Sizeof  (Mark ));  For ( Int I = 0 ; I <m; I ++ ) {Scanf (  "  % D  " , & X, & Y ,&SD); speed [x] [Y] = Speed [y] [x] = SD;} scanf (  "  % D  " ,& Q );  While (Q -- ) {Scanf (  "  % D  " , & Begin ,& End); Mark [begin] = 1  ; Min = Min_speed = 1000010  ; Max_speed =- 1  ; DFS (begin, max_speed, min_speed );  If (Min < 1000010  ) Printf (  "  % D \ n  "  , Min );  Else Printf ( "  -1 \ n "  );}}  Return   0  ;} 

 

(2) I checked the report on solving the problem online, and I used the query set + brute force search .......

Query set: merges connected cities into a set.

Sort: sort by traffic speed limit from small to large

Brute-force search: If a set has both the starting and ending positions, and the sorting is sorted from small to large, therefore, you can simply use the last put speed minus the first put speed. The Brute Force Search times to find out the most qualified ones.

View code

# Include <stdio. h> # Include <Stdlib. h> # Include <Algorithm> Using  Namespace  STD; typedef  Struct  {  Int  Start, end;  Int  Length;} edge; edge [  1010  ];  Int Root [ 210  ];  Bool  CMP (edge a, edge B ){  Return A. Length <B. length ;}  Int Find_root ( Int  A ){  If (A = root [a]) Return  A;  Return Root [a] = Find_root (root [a]);}  Int  Main (){  Int  N, m, Q;  Int  Begin, end; Int  Min;  While (Scanf ( "  % D  " , & N, & M )! = EOF ){  For ( Int I = 0 ; I <m; I ++ ) Scanf (  "  % D  " , & Edge [I]. Start, & edge [I]. End ,&Edge [I]. Length); sort (edge, Edge + M, CMP); scanf (  "  % D  " ,& Q );  While (Q -- ){  Int  FX, FY; min = 1000010  ; Scanf (  "  % D  " , & Begin ,& End );  For ( Int K = 0 ; K <m; k ++ ){  Int  I;  For (I = 0 ; I <= N; I ++ ) Root [I] = I;  For (I = K; I <m; I ++) {FX = Find_root (edge [I]. Start); FY = Find_root (edge [I]. End );  If (FX! = FY) Root [FY] = FX;  If (Find_root (BEGIN) = Find_root (end ))  Break  ;}  If (I = m)Break  ;  If (Edge [I]. Length-edge [K]. Length < Min) min = Edge [I]. Length- Edge [K]. length ;}  If (Min < 1000010  ) Printf (  "  % D \ n  "  , Min );  Else Printf ( " -1 \ n  "  );}}  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.