Link: Sicily 1090
Ideas:
Simple Minimum Spanning Tree problem. Use the prim algorithm here. Use the visited array to record whether each node has been accessed, that is, whether it is already in the minimal spanning tree. Each time, a node with the smallest key value is extracted from the node in the minimal spanning tree and put it into the spanning tree. The key value indicates the minimum distance between the node and the point set in the generated tree. Each time a node is added, the key value of the adjacent node is updated.
Code:
# Include <iostream> # include <queue> # include <stdio. h> # include <memory. h> using namespace STD; const int max = 501; const int max_len = 65537; int prim_get_longest (int n, int adj [] [Max]); int main () {int testcases; CIN> testcases; while (testcases --) {int N; CIN> N; int adj [Max] [Max]; for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) {CIN> adj [I] [J] ;}} cout <prim_get_longest (n, adj) <Endl; If (T Estcases> 0) cout <Endl;} return 0;} int prim_get_longest (int n, int adj [] [Max]) {// post: using prim algorithm to find the minimal spanning tree, // and return the longest edge in the tree.int longest = 0; bool visited [Max]; int key [Max]; memset (visited, false, sizeof (visited); For (INT I = 2; I <= N; ++ I) {key [I] = adj [1] [I];} visited [1] = true; // rootfor (INT I = 1; I <n; ++ I) {int U, min = max_len; f Or (Int J = 2; j <= N; ++ J) {If (! Visited [J] & Key [J] <min) {min = Key [J]; u = J ;}} longest = max (longest, min ); visited [u] = true; For (INT v = 2; v <= N; ++ v) {If (! Visited [v] & adj [v] [u] <key [v]) {key [v] = adj [v] [u] ;}} return longest ;}