Second, the chart algorithm
1. Minimum span
A.Prim Algorithm:
Procedure Prim (V0: Integer);
VAR
Lowcost, Closest: array [1..maxn] of integer;
I, J, K, min: integer;
Begin
For i: = 1 to n DO
Begin
Lowcost [I]: = COST [V0, I];
Closest [i]: = V0;
END;
For i: = 1 to n - 1 do
Begin
{Looking for the nearest point of the spangent tree K}
MIN: = Maxlongint;
For j: = 1 to n DO
IF (Lowcost [J]
Begin
MIN: = lowcost [j];
K: = J;
END;
Lowcost [K]: = 0; {Add the vertex k to the spanning tree}
{Generated tree Add a new side K to Closest [K]}
{Corrected Lowcost and CloseSt value}
For j: = 1 to n DO
IF cost [k, j] Begin Lowcost [J]: = COST [K, J]; Closest [J]: = K; END; END; End; {prim} B.kruskal algorithm: (greed) Sequentially in the order of weight, if you do not form a loop, add the minimum spanning tree. Function Find (v: integer): integer; {Return to the set of vertices v} VAR i: integer; Begin i: = 1; While (i <= n) and (not v in vset [i]) do INC (I); IF i <= n Then Find: = i Else Find: = 0; END; Procedure kruskal; Var Tot, I, J: Integer; Begin For i: = 1 to n DO vset [i]: = [i]; {initialization definition N set, the i-th collections contain an element I} P: = n - 1; Q: = 1; Tot: = 0; {P is the number of edges that is still added, Q is a sideset pointer} Sort; {Incurrently incrementing all edges, existing in E [i], e [i] .v1 and e [i] .v2 is the serial number of the two vertices connected to the side I, E [i] .le Length of the first side} While P> 0 do Begin I: = FIND (e [q] .v1); J: = FIND (e [q] .v2); IF i <> j Then Begin INC (TOT, E [q] .le); VSET [I]: = vset [i] vset [j]; vset [j]: = []; DEC (P); END; INC; END; Writeln (Tot); END; 2. Minimum path A. The label method for solving the shortest path: VAR A: array [1..maxn, 1..maxn] of integer; B: array [1..maxn] of integer; {b [i] pointing to the shortest path of the top i to the source point} Mark: array [1..maxn] of boolean; PROCEDURE BHF; VAR BEST, BEST_J: Integer; Begin Fillchar (Mark, SizeOf (Mark), False; Mark [1]: = true; b [1]: = 0; {1 source point} Repeat BEST: = 0; For i: = 1 to n DO IF Mark [i] Then {points for each of the shortest paths have been calculated For j: = 1 to n DO IF (Not Mark [J]) and (a [i, j]> 0) THEN IF (Best = 0) OR (B [i] a [i, j] Begin BEST: = B [i] a [i, j]; BEST_J: = J; END; IF best> 0 THEN Begin B [Best_J]: = BEST; MARK [BEST_J]: = true; END; Until Best = 0; End; {bhf} B.FLOYED algorithm solves the shortest path between all vertices: Procedure floyed; Begin For i: = 1 to n do for j: = 1 to n DO IF a [i, j]> 0 THEN p [i, j]: = i ELSE P [I, J]: = 0; {P [i, j] represents the front discharge point of J to J on the shortest path of j Fork: = 1 to n DO {enumeration intermediate node} For i: = 1 to n do for j: = 1 to n DO