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]
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] Lowcost [J]: = COST [K, J]; Closest [J]: = K; END; END; End; {prim} B.kruskal algorithm: (greed) Remove the edges in the figure in order, if the loop is not formed, 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 the edge set pointer} Sort; {Incrementing all sorts by weight, existing in E [i], e [i] .v1 and e [i] .v2 is two vertices connected to the side I Serial number, E [i] .le is the length of the first side of the first} 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 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 {point 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] 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] indicates that before i to j's shortest path on J Dialect point} Fork: = 1 to n DO {enumeration intermediate node} For i: = 1 to n DO For j: = 1 to n DO