HDU 1598 find the most comfortable road (enumeration + camerkar minimal spanning tree)

Source: Internet
Author: User
Tags define local
Find the most comfortable road

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


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 221 31 2

 

Sample output10

 

Authorailyanlu

 

Source HDU 2007-spring Programming Contest-warm up (1) idea: Build a minimum tree with different edges as the starting point, and find the length of the maximum edges and the minimum edges among them. You can! Code:
// # Define local # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; const int INF = 1000000; int father [202]; struct node {int A, B, C; bool operator <(node const & bb) const {return BB. c> C; // small to large} TT [1005]; void Init (int n) {for (INT I = 1; I <= N; I ++) father [I] = I;} int fin (INT X) {While (X! = Father [x]) x = Father [X]; return X;} void unin (int A, int B) {FATHER [a] = B;} int main () {# ifdef local freopen ("test. in "," r ", stdin); # endif int n, m; while (scanf (" % d ", & N, & M )! = EOF) {for (INT I = 1; I <= m; I ++) scanf ("% d", & TT [I]. a, & TT [I]. b, & TT [I]. c); sort (TT + 1, tt + (m + 1); int Q; scanf ("% d", & Q); While (Q --) {int en, St; scanf ("% d", & St, & en); int ans = inf; For (INT I = 1; I <= m; I ++) {Init (n); For (Int J = I; j <= m; j ++) {int Fr = fin (TT [J]. a); int to = fin (TT [J]. b); If (fr! = To) unin (to, fr); Fr = fin (ST); To = fin (en); If (Fr = to) {ans = min (ANS, TT [J]. c-TT [I]. c); break ;}}if (ANS = inf) ans =-1; printf ("% d \ n", ANS) ;}} return 0 ;}

 

HDU 1598 find the most comfortable road (enumeration + camerkar minimal spanning tree)

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.