Ultraviolet A 10048-audiophobia (Floyd deformation)

Source: Internet
Author: User

An undirected connected graph and edge weight are given. The objective is to find the path with the smallest edge weight in the path from one point to another. The output is the maximum edge weight of the path.

Because it is a path problem between two points, and the data size is small (only 100), we consider using the Floyd algorithm.

However, it does not require the shortest circuit between two points in the traditional Floyd request. However, by understanding the principle of the Floyd algorithm, we can find that the concept of Floyd can be used to solve this problem:

For any path I-> J that contains at least two edges, there must be an intermediate vertex K, make the total length of I-> J equal to the sum of I-> K and K-> J. Because there may be multiple paths, the minimum value must be obtained.

For any path I-> J that contains at least two edges, there must be an intermediate vertex K, so that the maximum values of I-> J are equal to the maximum values of I-> K and K-> J. Because there may be multiple paths, the minimum value must be obtained.

This is the above principle.

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF 0x3fffffff # define MEM (a) memset (A, 0, sizeof ()) int N, S, Q; int d [105] [105]; void Floyd () {for (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {d [I] [J] = min (d [I] [J], max (d [I] [K], d [k] [J]) ;}}} int main () {// freopen ("out.txt", "W", stdout); int Kase = 1; while (scanf ("% d", & N, & S, & Q )! = EOF) {If (n = 0 & s = 0 & Q = 0) break; MEM (d); For (INT I = 0; I <= N; I ++) {for (Int J = 0; j <= N; j ++) d [I] [J] = inf;} int A1, a2, A3; For (INT I = 0; I <s; I ++) {scanf ("% d", & A1, & A2, & A3); D [a1] [a2] = A3; d [a2] [a1] = A3;} Floyd (); If (Kase! = 1) printf ("\ n"); printf ("case # % d \ n", Kase ++); For (INT I = 0; I <q; I ++) {int U, V; scanf ("% d", & U, & V); If (d [u] [v] = inf) printf ("no path \ n"); else printf ("% d \ n", d [u] [v]) ;}} return 0 ;}


Ultraviolet A 10048-audiophobia (Floyd deformation)

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.