HDU 1598 find the most comfortable road

Source: Internet
Author: User

Let's talk about Chinese questions directly!

Idea: I'm excited when I look at the question. It seems to be a problem related to the maximum stream. But after carefully reviewing the question, I find that you need to find the path with the smallest difference between the maximum speed and the minimum speed! The difference between the maximum and the minimum is the smallest, so we can think of the greedy problem. Another point is that we can reach the destination, which means we need to connect the given start point and end point, so we can consider and check the idea of the set!

So the general idea of this question can be determined as: We can sort the weights of all edges, and then enumerate all vertices from 0 to connect all vertices, check whether "find (a)" is equal to "find (B)". If it is equal to "find (B)", we can take the maximum value and the minimum value as the difference!

AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF limit int f [205], n; struct P {int U, V, w;} num [1005]; bool CMP (P x, p y) {return X. W <Y. w;} int find (int x) {If (X! = F [x]) x = find (F [x]); Return X;} int main () {int M, T, I, J, K, Minn; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <m; I ++) scanf ("% d", & num [I]. u, & num [I]. v, & num [I]. w); sort (Num, num + M, CMP); scanf ("% d", & T); While (t --) {Minn = inf; int A, B; scanf ("% d", & A, & B); for (I = 0; I <m; I ++) // starts enumeration from 0th points, find the optimal solution for all cases {for (k = 1; k <= N; k ++) // each enumeration determines a new query set f [k] = K; For (j = I; j <m; j ++) // start from vertex I to find the path {int X, Y; X = find (Num [J]. u); y = find (Num [J]. v); If (X! = Y) f [y] = x; If (find (A) = find (B) {If (Minn> num [J]. w-num [I]. w) Minn = num [J]. w-num [I]. w; break;} If (j = m) // The first time we connect to all vertices, we find that there is no path from A to B, so we do not need to find a new break ;}} if (Minn = 100000000) printf ("-1 \ n"); else printf ("% d \ n", Minn) ;}} 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.