Obviously, the Minimum Spanning Tree template generates multiple questions.
[CPP] View plaincopy
-
- # Include <bits/stdc ++. h>
-
- UsingNamespace STD;
-
- Int N, K,;
-
- Int Dist [120], M [120] [120];
-
- Bool P [120];
-
- Void prim ()
-
- {
- For (Int I = 1; I <= N; I ++)
-
- {
-
- If (! P [I])
-
- {
-
- Int min = 100020;
-
- For (Int J = 1; j <= N; j ++)
-
- {
- If (P [J] & M [I] [J] <min)
-
- Min = m [I] [J];
-
- }
-
- Dist [I] = min;
-
- }
-
- }
-
- For (Int I = 1; I <= n-k; I ++)
-
- {
- Int min = int_max, K = 0;
-
- For (Int J = 1; j <= N; j ++)
-
- {
-
- If (! P [J] & Dist [J] <min)
-
- {
-
- Min = DIST [J];
-
- K = J;
- }
-
- }
-
- If (k = 0)
-
- Return;
-
- P [k] =True;
-
- For (Int J = 1; j <= N; j ++)
-
- {
- If (! P [J] & Dist [J]> M [k] [J])
-
- Dist [J] = m [k] [J];
-
- }
-
- }
-
- }
-
- Int main ()
-
- {
-
- Memset (p,False,Sizeof (p ));
-
- Scanf ("% D", & N, & K );
- For (Int I = 1; I <= K; I ++)
-
- {
-
- Scanf ("% D", & );
-
- Dist [a] = 0;
-
- P [a] =True;
-
- }
-
- For (Int I = 1; I <= N; I ++)
-
- For (Int J = 1; j <= N; j ++)
- Scanf ("% D", & M [I] [J]);
-
- Prim ();
-
- Int sum = 0;
-
- For (Int I = 1; I <= N; I ++)
-
- Sum + = DIST [I];
-
- Printf ("% D \ n", sum );
-
- Return 0;
-
- }