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