Sicily 1090. Highways solution report

Source: Internet
Author: User

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 ;}

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.