Acm1598 and query Set Method

Source: Internet
Author: User
Find the most comfortable roadproblem descriptionxx star to 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 221 31 2 Sample output10 and check the great power of the set is waiting for us to explore, here again let us brainstorm.
1 # include <iostream> 2 # include <algorithm> 3 # include <cstring> 4 using namespace STD; 5 const int oo = 11111111; 6 const int n = 220; 7 class COOR 8 {9 public: 10 int S, E, speed; // represents the starting point and end point of the Line 11 friend bool operator <(coor a, coor B) 12 {13 return (. speed <B. speed); // sort from small to large, saving time by using greedy algorithms 14} 15}; 16 class comfort 17 {18 public: 19 comfort (int n, int m) // constructor 20 {21 num = N; 22 side = m; 23 p = new int [n + 1]; // Temporary array space allocation 24 edge = new COOR [M + 1]; 25} 26 ~ Comfort () 27 {28 Delete [] P; 29 Delete [] edge; 30} 31 32 void Inui () 33 {34 for (INT I = 0; I <= num; I ++) 35 p [I] = I; 36} 37 void inputedge () 38 {39 for (INT I = 0; I <side; I ++) 40 CIN> edge [I]. s> edge [I]. e> edge [I]. speed; 41 sort (edge, edge + side); // sort 42} 43 int find (int x) 44 {45 if (x = P [x]) return X; 46 Return P [x] = find (P [x]); // compression path 47} 48 void Union (COOR) 49 {50 int fa = find (COOR. s); 51 int Fb = Find (COOR. E); 52 If (Fa! = FB) P [fa] = FB; 53} 54 void dealpart (INT q) // The purpose of the following code is to determine the maximum and minimum values from the start point to the end point path 55 // a detailed explanation: first, I starts from scratch, and then the following cycle j starts from I, after a round of search, you should find the same path as the maximum value. If the first round cannot be found, it will inevitably be different. The following problems have been solved. 56 // after the maximum value can be determined in the first round, the cycle starts until the minimum value is determined. When the start point I advances forward, the minimum value is found when the start point and the end point are not connected. 57 {58 While (Q --) 59 {60 scanf ("% d", & START, & End); 61 less = Oo; 62 for (INT I = 0; I <side; I ++) 63 {64 Inui (); 65 int temp = Oo; 66 for (Int J = I; j <side; j ++) 67 {68 Union (edge [J]); // each time a union is connected until the start point and end point are connected, the maximum value is determined first, then determine the minimum value 69 int x = find (start); 70 int y = find (end); 71 If (x = y) 72 {73 temp = edge [J]. speed-edge [I]. speed; 74 break; 75} 76} 77 If (temp <less) 78 less = temp; 79 if (less = 0) break; // if less is already zero, you do not need to continue the loop, because there is no shorter than it, saving time 80 If (less = OO) break; // if no exact value is found in the first round of search, the two points are not connected, so they exit the loop directly, save time 81} 82 If (less = OO) cout <-1 <Endl; 83 else cout <less <Endl; 84} 85} 86 PRIVATE: 87 int less; 88 int * P; 89 int num, side; 90 COOR * edge; 91 int start, end; 92}; 93 int main () 94 {95 int N, m, Q; 96 while (CIN> N> m) 97 {98 comfort object (n, m); 99 object. inputedge (); 100 CIN> q; 101 object. dealpart (Q); 102} 103 return 0; 104}

 


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.