Uva10048 audiophobia (Floyd)

Source: Internet
Author: User

Question: there are multiple paths between any two points. The maximum weight of each path is different. In other words, from one point to another, all the edges passing through, the largest value is the maximum permission of this path.

The maximum permission of the path with the smallest maximum weight in the left and right paths between any two points.

Experience: Because there are multiple groups of operations, the maximum number of operations can reach 10000. If the DFS before each operation, it must have timed out, so I thought of the Floyd algorithm. The idea is correct, but the idea of details is wrong, so I thought that I could traverse all the points with DFS and save the answer to the two-dimensional array, but this method times out. Because the maximum number is 100 points, if each point requires DFS, the number is very large. Finally, I went back to the Floyd algorithm and carefully thought about the Floyd algorithm for finding the shortest path. The main idea is to insert a vertex between two points. When finding the shortest path, if you insert a vertex so that (s, k) + (K, T) is shorter than the path from S to T, then insert the vertex.

If you insert a vertex to reduce the maximum weight from S to T, insert This vertex

Therefore, the problem is how to determine whether the maximum weight is reduced:

First, the insertion point must satisfy the conditions that the maximum weight from S to k is smaller than from S to T, and the value from K to T is smaller than (S, T ), after this point is inserted, the maximum weight is reduced.

Second, make sure that after the insertion, the slave (S, T) is the largest right, then the (S, K) and (K, j) must be) the big value is assigned to (S, T)

This is a DP idea

The Code is as follows:

Note: In the end, the map stores the smallest maximum weight from S to T, which is the same as the Floyd shortest path algorithm.

#include <cstdio>#include <algorithm>using namespace std;const int inf = 1000000;const int MAX = 105;int C, S, Q;int map[MAX][MAX];int s, t;int c1, c2, d;void floyd() {    int i, j, k;    for ( k = 1; k <= C; ++k )         for ( i = 1; i <= C; ++i )            for ( j = 1; j <= C; ++j )                 if ( i != j && i != k && j != k && map[i][k] < map[i][j] && map[k][j] < map[i][j] ) map[i][j] = max(map[i][k], map[k][j]);}int main(){    int icase = 1;    bool f = false;    while ( scanf("%d%d%d", &C, &S, &Q) != EOF && !( C==0 && S==0 && Q==0 ) ) {        if ( f ) printf("\n");        f = true;        for ( int i = 0; i <= C; ++i )             for ( int j = 0; j <= C; ++j ) map[i][j] = inf;        for ( int i = 1; i <= S; ++i ) {            scanf("%d%d%d", &c1, &c2, &d);            map[c1][c2] = map[c2][c1] = d;        }        floyd();        printf("Case #%d\n", icase++);        for ( int i = 1; i <= Q; ++i ) {            scanf("%d%d", &s, &t);            if ( map[s][t] == inf ) printf("no path\n");            else printf("%d\n", map[s][t]);        }    }}

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.