Prim (addition method)
1. Select either of them (generally 1). As the starting point, set the distance from the minimum spanning tree to 0 (in fact, it is to select a vertex and materialized the tree ),.
2. select the shortest distance from the least Spanning Tree among all unselected points, accumulate the distance, and mark it as selected. if all are selected, the Minimum Spanning Tree (total route length) is obtained ).
3. Update the vertex adjacent to this vertex "distance from the smallest Spanning Tree". 2 is returned.
# Include <cstdio> # include <cstring> # include <vector> # include <algorithm> using namespace STD; # define n 1003 # define INF 0x3f3f3fstruct node {int V, W; node () {} node (INT _ v, int _ w): V (_ v), w (_ w) {}}; vector <node> G [N]; int n, m, d [N]; bool vis [N]; int prim () {memset (VIS, false, sizeof (VIS); memset (D, 0x3f, sizeof (d); int ans = d [1] = 0; For (INT I = 0; I <n; I ++) {int K = 0, mi = inf; For (Int J = 1; J <= N; j ++) if (! Vis [J] & D [J] <mi) MI = d [J], K = J; If (k = 0) break; vis [k] = true; ans + = mi; For (Int J = 0, U; j <G [K]. size (); j ++) if (! Vis [U = G [k] [J]. v] & D [u]> G [k] [J]. w) d [u] = G [k] [J]. w; // It is very similar to Dijkstra, but here the relaxation operation is changed to update} // here d indicates the distance from the tree return ans; /// return the edge length of the Minimum Spanning Tree} int main () {While (scanf ("% d", & N, & M) = 2) {for (INT I = 0; I <= N; I ++) g [I]. clear (); For (INT I = 0, A, B, C; I <m; I ++) {scanf ("% d", &, & B, & C); G [A]. push_back (node (B, c); G [B]. push_back (node (a, c);} printf ("% d \ n", Prim ();} return 0 ;}
Kruskal)
1. Add all vertices to and query the set. Each vertex is an independent set.
2. Sort all edges by length.
3. take out the smallest edge and judge whether the two vertices are in the same set until the edge set is empty. if yes, discard. 3 is returned. if not, add the left vertex to the right vertex set (left and right are indifferent ~), 3.
4. If the edge set is empty, the Minimum Spanning Tree is obtained.
The query set uses path compression by convention.
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> using namespace STD; # define n 1002 struct node {int U, V, W; node () {} node (INT _ u, int _ v, int _ w): U (_ u), V (_ v), w (_ w) {}}; vector <node> edge; int N, M, F [N]; bool CMP (const node & X, const node & Y) {return X. W <Y. w;} int find_set (int x) {If (F [x] = x) return X; return f [x] = find_set (F [x]);} int Kruskal () {sort (edge. begin (), edge. end (), CMP); For (INT I = 1; I <= N; I ++) f [I] = I; /// add all vertices to and query the set. It is an independent set of int ans = 0; For (INT I = 0, U, V, W; I <edge. size (); I ++) {// after sorting, you just need to take it and then u = edge [I]. u, v = edge [I]. v, W = edge [I]. w; u = find_set (u), V = find_set (V); If (u = V) continue; F [u] = V; // This is random, although it may cause efficiency instability, it will be the same after one path compression .. ans + = W;} return ans;} int main () {While (scanf ("% d", & N, & M) = 2) {edge. clear (); For (INT I = 0, A, B, C; I <m; I ++) {scanf ("% d", &, & B, & C); edge. push_back (node (A, B, C); // the two ends of the vertex are equal, insert once} printf ("% d \ n", Kruskal ());} return 0 ;}