Poj 1789 prim algorithm

Source: Internet
Author: User

The sum of the Minimum Spanning Tree Length of the graph is required. The edge set indicates the edge with the minimum distance found.

InitialD array, indicating the minimum distance from the vertex to this edge setIf d [I] = 0, the I point is in the edge set.

Dis two-dimensional array, indicating the distance between two points

Initially, d [I] indicates the distance from I to 0, and D [0] = 1

Find the minimum value D [Min] In D [I] and add the min point to the edge set. For all vertices except the edge set J, if the distance from J to Min is smaller than that from D [J], d [J] = dis [J] [Min], (the distance from the original d [J] to 0, in this way, d [J] is always the minimum distance to the edge set)

# Include <iostream> # define Max 2001 Char Types [Max] [8]; Char Dis [Max] [Max]; Char D [Max]; # define INF 127 Int Prim ( Int N ){ Int I, J, K, Ti; Int Mindis, total = 0; d [0] = 0;For (I = 1; I <n; I ++) d [I] = dis [I] [0]; For (I = 1; I <n; I ++) {mindis = inf; // Find the minimum weight          For (K = 0; k <n; k ++ ){ If (D [k] & D [k] <mindis) {Ti = K; mindis = d [k] ;}} total + = mindis; d [Ti] = 0; For (J = 0; j <n; j ++ ){ If (D [J] & dis [J] [Ti] <D [J]) d [J] = dis [J] [Ti];} Return Total ;} Int Main (Int Argc, Char * Argv []) { // Freopen ("I: // t.txt", "r", stdin );  Int C, I, J, K; While (Scanf (" % D ", & C ){ For (I = 0; I <C; I ++) {scanf (" % S ", Types [I]); For (J = 0; j <I; j ++ ){ Int DIF = 0; For (K = 0; k <7; k ++) If (Types [J] [k]! = Types [I] [k]) DIF ++; DIS [I] [J] = dis [J] [I] = DIF ;}} printf (" The highest possible quality is 1/% d. \ n ", Prim (c ));} 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.