Algorithm

xiaoxiao2021-03-06  38

First, the number of algorithms

1. Seeking two maximum number of conventions

Function GCD (A, B: Integer): Integer; Begin IF B = 0 THEN GCD: = a else GCD: = GCD (B, A MOD B); END;

2. Seeking two minimum common multiple

Function LCM (A, B: INTEGER): Integer; Begin IF A 0 DO INC (LCM, A); END;

3. Justice

A. Decision of a number is the number: Function Prime (N: Integer): boolean; var i: integer; begin for i: = 2 to trunc (SQRT (n)) do if n mod i = 0 THEN Begin PRIME: = false; exit; end; prime: = true;

B. Judging whether the number within the longint range is the number of prime (including the number table within 50000): Procedure getPrime; Var i, j: longint; p: array [1..50000] of boolean; begin Fillchar (p, sizeof p), true); p [1]: = false; i: = 2; while i <50000 do begin if p [i] the begin j: = i * 2; while j <50000 do begin p [j]: = false; INC (j, i); end; end; inc (i); end; l: = 0; for i: = 1 to 50000 DO if p [i] the begin inc (L); Pr [l] : = i; end; end; {getprime} function prime; var i: integer; begin prime: = false; for i: = 1 to l do if pr [i]> = x Then Break Else IF X MOD PR [I] = 0 THEN EXIT; PRIME: = true; end; {prime}

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 beg, lowcost I]: = COST [V0, I]; CloseSt [I]: = V0; End; for i: = 1 to n-1 do beg {Looking for the nearest point of the spound tree: = maxlongint; for J: = 1 to n DO if (Lowcost [J] 0) THEN BEGIN MIN: = Lowcost [J]; K: = J; End; Lowcost [K]: = 0 {Add a vertex k to the spanning tree} {spanning tree to add a new side K to Closest [k]} {corrected the lowcost and closest values ​​of each point} for j: = 1 to n DO if cost [k, j]

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;

Procedure Kruskal; Var Tot, I, J: Integer; Begin for i: = 1 to n DO vset [i]: = [i]; {Initialization Definition N Collection, the i-th collection contains an element I} P: = N-1; q: = 1; Tot: = 0; {P is the number of edges that are still added, Q is the edge set pointer} sort; {Single to all edges, exemplified 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 is the length of the first side 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] .len); vset [i]: = vset [i] Vset [J]; Vset [J]: = []; DEC (P); End; Inc (Q); End; Writeln (Tot);

2. Minimum path

A. Number method Solution Solution Source point shortest path: var A: array [1..maxn, 1..maxn] of integer; b: array [1..maxn] of integer; {b [i] pointing top i to The shortest path of the source} Mark: array [1..maxn] of boolean;

Procedure BHF; VAR BEST, BEST_J: Inteder; Begin Fillcha (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 calculated shortest path} 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] 0 THEN BEGIN B [BEST_J]: = BEST; MARK [BEST_J]: = true; end; until best = 0; end; {bhf} B.FLOYED algorithm solving the shortest path between all vertices: Procedure Floyed; beginfor i: = 1 to n DOFOR J: = 1 to n doif a [i, j]> 0 THEN P [i, j]: = I else p [i, j]: = 0; {p [i , j] means I to J's shortest path on J's pre-discrete point} for K: = 1 to n DO {enumeration intermediate node} for i: = 1 to n do for j: = 1 to n DO if a [i, k] a [j, k]

C. Dijkstra algorithm:

Var A: Array [1..maxn, 1..maxn] of integer; b, pre: array [1..maxn] of integer; {pre [i] refers to the shortcuting point of I on the shortest path} Mark: array [1..maxn] of boolean; procedure dijkstra (V0: integer); Begin Fillchar (Mark, Sizeof (Mark), False); for i: = 1 to n Do Begin D [i]: = a [v0, i ]; IF D [i] <> 0 THEN PRE [I]: = V0 else pre [I]: = 0; end; mark [v0]: = true; repeat {Each cycle is added to 1 episode Point and adjust the parameters of other nodes} min: = maxint; u: = 0; {u Record from 1 episode nearest node} for i: = 1 to n DO if (Not Mark [i]) and (d [ i] 0 THEN BEGIN MARK [U]: = true; for i: = 1 to n DO if (Not Mark [ I]) and (a [u, i] d [u]

Procedure longlink; var: array [1..maxn, 1..maxn] of boolean; beginfillchar (t, sizeof (t), false; fork: = 1 to n DOFOR i: = 1 to n do for J: = 1 to N DO T [I, J]: = T [I, J] OR (T [I, K] and Ti T [K, J]); END;

4. Non-map connected component

A. Depth Priority Procedure DFS (Now, Color: Integer); Begin for i: = 1 to n DO if A [now, i] and c [i] = 0 THEN BEGIN {Node I Dye} C [i] : = Color; DFS (I, Color); End; end;

B width priority (seed staining)

5. Critical Path

Several definitions: The vertex 1 is the source point, n is a combo. a. Vertence event happen time VE [J], VE [J] = Max {VE [J] W [I, J]}, where VE (1) = 0; b. Vertex Event Time VL [ J], VL [J] - W [i, j]}, where VL (n) = ve (n); c. The earliest start time of the event EE [i], if the side i is , then EE [i] = ve [j]; d. The last time EL [i] is started at the end of the event, if the side i is indicated by , EL [i] = VL [ K] - w [j, k]; if EE [J] = EL [J], the activity J is a critical activity, and the path consists of key activities is a critical path. Solving method: a. From the source point to TOPSORT, determine if there is loop and calculate ve; b. From the exchange point to Tops, seeking VL; C. Calculate EE and EL;

6. Topology sort

The point of finding 0 is 0, and the other side of which is connected is deleted, and the process is repeated. For a few columns, any of the consecutive P items is positive, any Q item and the negative, if there is no existence, No. 7 is output. Loop problem

Euler loop (DFS) Definition: Only one loop through each side of the figure. (Mon)

Hamilton loop definition: The loop is only once through each vertex.

A painting feature condition: Figure is connected and the number of odds is 0 or 2.

9. Is there a negative circuit Bellman-Ford algorithm in the figure?

X [i], y [i], t [i] indicates the starting point, end point, and weight of the first edge of the clip. A total of n nodes and M strips. Procedure Bellman-Ford BeginFor i: = 0 to N-1 DO D [I]: = Infinitive; D [0]: = 0; for i: = 1 to n-1 DOFOR J: = 1 to m DO { Each side} IF D [X [J] T [J] D [J]: = D [X [J] T: = D [X [J]] T [J]; for I: = 1 TO M DOIF D [X [J] T [J]

10. Nth shortest path problem

* Second shortest path: Each side of the shortest path, each delete one, then ask the shortest path to the new map, take the shortest one of these paths to the second shortest path. * Similarly, the Nth shortest path can be solved on the basis of solving the N-1 shortest path.

[color = # 0000f] three, backpack problem [/ color]

* Some backpack problems can have greed method: calculate the PI / Wi data structure: w [i]: the weight of the i-th backpack; p [i]: The value of the i-th backpack;

1.0-1 Backpack: Each backpack can only use once or limited (convertible to once):

A. Ask for maximum weight. NOIP2001 packing problem has a box capacity of V (positive integer, O ≤ V ≤ 20000), and N items (O ≤ N ≤ 30), each item has a volume (positive integer). Requires from N items, if you have a thousand loading boxes, the remaining space of the box is minimized. l Search method procedure Search (k, v: integer); {Searching the kth item, the remaining space is V} Var I, J: Integer; Begin IF V = BEST THEN EXIT; {s [n] is the weight and} if K <= n Then Begin IF V> W [K] THEN Search (K 1, VW [k]); Search (k 1, v); end;

l DPF [i, j] Select several pieces of the previous I to put the size of the J. is a Boolean. Implementation: Transformation of optimization problems into judgment problem F [i, j] = f [i-1, jw [i]] (w [i] <= j <= v) boundary: F [0 ,0]: = true.for i: = 1 to n DOFOR J: = W [i] to v DO f [i, j]: = f [i-1, jw [i]]; optimization: current status only with the previous stage The status is related to one dimension. F [0]: = true; for i: = 1 to n DO beginf1: = f; for j: = w [i] to v doif f [jw [i]] THEN F1 [J]: = true; f: = F1; End; b. Sign in the maximum value of you can put. F [i, j] is the maximum value that the previous J backpack can obtain for the capacity of i. F [i, j] = max {f [i - w [j], j-1] p [j], f [i, j-1]}

C. Ask the number of applications that is full. DP: Procedure Update; Var J, K: Integer; Begin C: = A; For J: = 0 to N DO if A [J]> 0 THEN IF J NOW <= n THEN INC (C [J Now] , A [J]); A: = C; END;

2. Repeatable backpack

A seeking up to the weight can be placed. F [i, j] Select several pieces of the previous I to put it in a marker that is just J J. is a Boolean. The state transfer equation is f [i, j] = f [i-1, j - w [i] * k] (k = 1 .. J DIV W [i])

B. The maximum value that can be placed. USACO 1.2 score inflation conducts a competition, the total time T is fixed, there are several optional questions, each of the topics can be selected, each topic has a Ti (answer the time required for this question) and a Si (Solution of the score obtained by this question), now choose a number of questions, to solve the total time of these questions within T, the total score obtained, the maximum score. * Easy to think: f [i, j] = max {f [i- k * w [j], j-1] k * p [j]} (0 <= k <= I DIV W [J]) Where F [I, J] indicates that the maximum value that the front J table backpack can achieve when the capacity is i. * Implement: Beginfillchar (f, sizeof (f), 0); for i: = 1 to m DOFOR J: = 1 to n DO if i-problem [j] .time> = 0 THEN BEGIN T: = quobem [J ] .point f [i-problem [j] .time]; if T> f [i] THEN F [I]: = T; End; Writeln (f [f [m]); end.

C. Ask the number of applications that is full. Ahoi2001 Problem2 Seeking Natural Number N Nature Different Number and Expression Number. Idea, generate the arrangement of the coefficients of each rigidity, test in one by one, this is a form of a law. Procedure try; var i, j: integer; begin Cal; {This process calculates the calculation result of the current coefficient, now the result} if now> n Then exit; {During branch} if DEP = L 1 THEN Begin {Generate all coefficient} Cal; if now = n THEN INC (TOT); EXIT; END; for i: = 0 to n DIV PR [DEP] DO BEGIN XS [Dep]: = i; try (DEP 1) ; Xs [dep]: = 0; end; end; idea two, recursive search efficiency is higher Procedure Try (dep, rest: integer); Var i, j, x: integer; begin if (REST <= 0) OR DEP = L 1) Then Begin if rest = 0 THEN INC (TOT); EXIT; END; for i: = 0 to rest Div PR [DEP] DO TRY (DEP 1, REST-PR [DEP] * i) End; {main: try (1, n);}

Idea 3: You can use dynamic plan to solve the USACO1.2 Money Systemv item, the backpack capacity is N, and the total number of invested methods can be used. Transfer equation:

Procedure Update; Var J, K: Integer; Begin C: = A; For J: = 0 to N DO if A [J]> 0 Then For K: = 1 To N Div Now Do IF J Now * K <= N THEN INC (C [J Now * K], A [J]); A: = C; End; {main} begin read (now); {read into the weight of the first item} i: = 0; {a [i] is the total number of} when the backpack capacity is i} While i <= n do begin a [i]: = 1; INC (i, now); end; {Define the first item heavy integer multiple The weight a value is 1, as the initial value} for i: = 2 to v DobeGin read (now); Update; {Dynamic Update} End; Writeln (a [n]);

Fourth, ordering algorithm

1. Quick sort:

Procedure QSort (L, R: Integer); Var i, J, MID: Integer; Begin i: = L; J: = R; MID: = A [(L R) Div 2]; {{Put the current sequence in the middle Number of positions is defined as the middle number} Repeat While a [i] j; if l

B. Insert Sort:

Idea: Current a [1] .. a [i-1] has been ranked, now inserting a [i] to make A [1] .. a [i] is orderly. Procedure insert_sort; var i, j: integer; begin for i: = 2 to n Do begin a [0]: = a [i]; j: = i-1; while a [0] a [j] Then SWAP (a [i], a [ J]);

D. Sprinkle proporture bubble_sort; var i, j, k: integer; begin for i: = 1 to n-1 do for j: = n DOWNTO I 1 DO IF A [J]

E. Pile Sort: Procedure Sift (i, m: integer); {Adjusting the subtree of I root Become a pile, m is the total number of nodes} var k: integer; begin a [0]: = a [i]; K: = 2 * i; {The left child of the node i in the fully binary tree is 2 * i, the right child is 2 * i 1} while k <= m do begin if (k

Procedure Heapsort; Var J: Integer; Begin for J: = N DIV 2 Downto 1 Do Sift (J, N); for J: = N DOWNTO 2 Do Begin Swap (A [1], A [J]); SIFT ( 1, J-1); end; end;

F. Multiple Sort {A is a sequence list, TMP is an auxiliary array} Procedure Merge (VAR A: LISTTTYPE; P, Q, R: Integer); {Sorted Subsequent A [p..Q] and A [ q 1..r] combined with an orderly TMP [p "VAR I, J, T: Integer; TMP: LISTTYPE; Begin T: = P; i: = p; {T is TMP pointer, I, J is a pointer of left and right subsequences} while (t <= r) DO Begin IF (i <= q) {left sequence has residual} and ((j> r) or (A [i] <= a [j])) {Meet the requirements of the current element of the left sequence} THEN Begin TMP [T]: = a [i]; inc (i); end else beg [tmp [t]: = a [ J]; inc (j); end; inc; end; for i: = p to r do a [i]: = TMP [i]; end; {merge}

Procedure merge_sort (var A: listType; p, r: integer); {Merge Sorting a [p..r]} var q: integer; Begin IF P <> r the begin Q: = (p r-1) DIV 2; MERGE_SORT (A, P, Q); MERGE_SORT (A, Q 1, R); MERGE (A, P, Q, R); end; end; {main} beginmerge_sort (a, 1, n); end .G. Boundary sorting ideas: Sort each with a low to high to each element

V. High precision calculation

Definition of high precision: Type HP = array [1..maxlen] of integer;

1. High precision addition

Procedure Plus (A, B: HP; VAR C: HP); Var I, Len: Integer; Beginfillcha (C, SizeOf (C), 0); IF A [0]> B [0] THEN LEN: = A [ 0] Else Len: = B [0]; for i: = 1 to Len Do Begin Inc (C [I], A [i] B [I]); IF C [I]> 10 THEN BEGIN DEC (C [i], 10); INC (C [i 1]); end; {carry} end; if c [len 1]> 0 THEN INC (LEN); C [0]: = LEN; end; { Plus}

2. High-precision subtraction Procedure Substract (A, B: HP; VAR C: HP); Var I, Len: Integer; Begin Fillcha (C, SizeOf (C), 0); IF A [0]> B [0] THEN LEN : = a [0] else len: = b [0]; for i: = 1 to len do begin inc (C [i], A [i] -b [i]); if C [i] <0 THEN Begin Inc (C [I], 10); DEC (C [i 1]); end; while (len> 1) AND (C [LEN] = 0) DO DEC (LEN); C [0]: = Len;

3. High precision multiplied by low accuracy

Procedure Multiply (a: hp; b: longint; var c: hp); Var i, len: integer; Begin Fillchar (C, SizeOf (C), 0); LEN: = a [0]; for i: = 1 To Len Do Begin Inc (C [I], A [i] * b); Inc (C [i 1], (a [i] * b) DIV 10); C [i]: = C [i] MOD 10; End; INC; While (C [LEN]> = 10) Do Begin {Processing the highest position} C [LEN 1]: = C [len] DIV 10; C [len]: = C [LEN] MOD 10; INC; END; While (LEN> 1) AND (C [Len] = 0) Do Dec (LEN); {If necessary, adjust the len} C [0]: = Len; end; {multiply}

4. High precision multiplied by high precision

Procedure high_multiply (a, b: hp; var c: hp} var i, j, len: integer; begin Fillcha (C, SizeOf (C), 0); for i: = 1 to a [0] do for J: = 1 to b [0] Do Begin Inc (C [i J-1], A [i] * b [j]); INC (C [i j], c [i j-1] DIV 10 ); C [i j-1]: = C [i j-1] mod 10; end; len: = a [0] b [0] 1; while (len> 1) and (c [ LEN] = 0) DO DEC (LEN); C [0]: = LEN; END; 5. High precision divided by low accuracy

Procedure devide (a: hp; b: longint; var c: hp; var d: longint); {c: = a div b; d: = a mod b} var i, len: integer; begin Fillchar (C, Sizeof (c), 0); LEN: = a [0]; D: = 0; for i: = len downto 1 do begin d: = d * 10 a [i]; c [i]: = D DIV B ; D: = D mod b; end; while (len> 1) and (c [len] = 0) THEN DEC (LEN); C [0]: = LEN; END;

6. High precision divide high precision

Procedure high_devide (a, b: hp; var c, d: hp); Var i, len: integer; Begin Fillchar (C, SizeOf (C), 0); Fillchar (D, SizeOf (D), 0); LEN : = a [0]; D [0]: = 1; for i: = len downnto 1 do begin multiply (d, 10, d); d [1]: = a [i]; while (COMPARE (D, B)> = 0) DO {即 D> = B} Begin Subtract (D, B, D); INC (C [i]); end; end; while (len> 1) and (cs [len] = 0 ) DO DEC (LEN); C.1: = LEN; END;

Sixth, the traversal of the tree

1. Required sequence in pre-sequence

Procedure SOLVE (Pre, MID: String); var i: integer; begin if (pre= '' ') or (MID =' '') THEN EXIT; I: = POS (Pre [1], MID); Solve (Copy (Pre, 2, I), Copy (MID, 1, I-1)); SOLVE (COPY (Pre, i 1, Length (Pre) -i), Copy (MID, I 1, LENGTH (MID) -I)); post: = post pre [1]; {plus root, post after recursive end traversal traversal} END;

2. Present sequence prequet sequence

Procedure Solve (MID, Post: String); var i: integer; begin if (MID = '' ') or (post =' '') THEN EXIT; I: = POS (Post [Length (post)], MID); pre: = pre post [length (post)]; {plus root, after recursive end, pre-sequence traversal} SOLVE (COPY (MID, 1, I-1), COPY (POST, 1, I-1)); Solve (Copy (MID, I 1, Length (MID) -i), COPY (POST, I, Length (POST) -I)); END; 3. One of known pre-sequences

Function OK (S1, S2: STRING): Boolean; Var i, L: Integer; P: Boolean; Begin OK: = true; l: = Length (S1); for i: = 1 to l do begin p: = false For J: = 1 to L DO if S1 [i] = S2 [J] THEN P: = True; if NOT; EXIT; End; End;

Procedure Solve (pre, post: string); var i: integer; begin if (pre = '' ') or (post =' '') THEN EXIT; I: = 0; Repeat Inc (i); Until OK (Copy (Pre, 2, I), COPY (POST, 1, I)); SOLVE (Pre, 2, I), COPY (POST, 1, I)); MIDSTR: = MIDSTR PRE [1] SOLVE (COPY (Pre, i 2, Length (Pre) -i-1), Copy (POST, I 1, Length (POST) -i-1);

Seven-based conversion

1. Any positive and integer

In addition to N

2. Realtive mutualization Mutiny

3. Negative number of credits: Design a program, read a base of a decimal number, and a base of the load, convert this decimal number to this load: -r∈ {-2, -3 , -4, ....- 20}

Eight all arrangement and combination

1. Arranged: (1..n) Procedure Solve (Dep: integer); var i: integer; begin if dep er; begin if dep = n 1 dam Writeln (s); exit; end; for i: = 1 to n Do if not used [i] the begin s: = s chr (I ORD ('' 0 ')); Used [I]: = true; SOLVE (DEP 1); s: = COPY (s, 1, Length (s) -1); used [i]: = false; end;

2. Combined generation (1..n selected all schemes of K number) Procedure Solve (dep, pre); var i: integer; begin if dep = K 1 dam Writeln (s); exit; End; for i: = 1 to n DO if (not used [i]) AND (i> pre) THEN BEGIN S: = S CHR (i ORD ('' 0 ')); USED [i]: = True; SOLVE (DEP 1, I); S: = COPY (S, 1, Length (S) -1); Used [I]: = false; end; end; nine. Find algorithm

40% off

Function Binsearch (KEYTYPE): Integer; Var Low, HIG, MID: INTEGER; Begin Low: = 1; HIG: = N; MID: = (Low HIG) DIV 2; While (a [MID] .Key < > k) AND (Low <= HIG) Do Begin IF A [MID] .Key> K Then Hig: = MID-1 Else Low: = MID 1; MID: = (Low HIG) DIV 2; End; IF Low> HIG THEN MID: = 0; binsearch: = MID; END;

2. Tree look

Binary Sort Tree: The value of each node is greater than the value of its left sub-tree, which is less than the value of its right tree. Find function treesrh (k: keytype): Pointer; VAR Q: Pointer; Begin Q: = root; while (q <> nil) and (q ^ .Key <> K) do if k

Ten, greedy

* Meeting issues (1) N activities have a start time and an end time, only one event is only in one activity, and the maximum number of activities is satisfied. Solution: Sort by the end time of each activity, the top priority is satisfied.

(2) The meeting room has the least free time.

(3) Every customer has a wish to pay for maximum profit.

(4) Conference rooms in total R, iile I need to use I meeting rooms, the cost is the same, and ask for maximum profit.

Eleven, backtracking framework

1. N Queens

Procedure try (i: byte); var j: byte; begin if i = n 1 dam print; EXIT; End; for j: = 1 to n DO if a [i] and b [j i] and c [Ji] THEN BEGIN X [I]: = J; A [J]: = false; b [j i]: = false; c [ji]: = false; try (i 1); A [J] : = True; B [i J]: = true; c [ji]: = true; end;

2.Hanoi Tower Hanno Tower

H (n) = 2 * h (n-1) 1 h (1) = 1 Initial all copper sheets are on the A column Procedure Hanoi (N, A, B, C: byte); {Put the nine copper The sheet moves from the A column to the C column to the C column} begin if n = 0 THEN EXIT; HANOI (N-1, A, C, B); {Move the above N-1 block from the A column to the c column B - column} Write (N, 'Moved from', A, 'to', C); Hanoi (N-1, B, A, C); {{passing the N-1 block on B passes a column Move to the c column END; the initial copper sheet is distributed on 3 columns, given the target column Goalh [1..N] Store three columns, NOWP's largest copper The column number and number, h [i, 0] are saved in the number of numbers on the i.

Procedure move (k, goal: integer); {Move the greatest in position to the target column Goal on Goal} Beginif K = 0 THEN EXIT; for i: = 1 to 3 do for j: = 1 to han [i, 0 ] Do IF H [i, J] = k Then Begin Now: = i; NOWP: = J; End; {Location of K Location} if now <> goal dam} is not moved to the target} Move (K-1 , 6-now-goal); {Leave first moved to the unused column} Writeln (K Moved from now to goal); h [Goal, H [Goal, 0] 1]: = h [now, NOWP]; h [now, nop]: = 0; INC (h [goal, 0]); dec (h [now, 0]); Move (k-1, goal); {Leave moved to the target } END;

Twelve, DFS framework

Division of NOIP2001

Procedure Work (dep, pre, s: longint); {The entrance is Work (1, 1, n)} {dep to the number of number of number of current trials, pre is the number of previous test places, S is the current remaining Total number} var j: longint; begin if s> = prehen INC (R); EXIT; END; for J: = pre To s Div 2 Do Work (dep 1, j, sj ); end; similar: procedure try; var i: integer; begin if DEP = K THEN BEGIN if Tot> = a [DEP-1] THEN INC (SUM); EXIT; END; for i: = A [DEP-1] to TOT DIV 2 Do Begin A [DEP]: = I; DEC (TOT, I); TRY (DEP 1); Inc (TOT, I); End; End; {Try}

Thirteen, BFS framework

IOI94 room problem Head: = 1; tail: = 0; while tail

Fifteen, data structure related algorithm

1. Link list of positioning functions

LOC (I: Integer): Pointer; {{Looking for the pointer of the i-th sideworth in the list} Procedure Loc (LINKLIST; I: Integer): Pointer; VAR P: ​​POINTER; J: Integer; beginp: = L. HEAD; J: = 0; if (i> = 1) AND (i <= l.len) THEN WHILE J

Procedure INSERT (L: Linklist; i: integer; x: datatype); VAR P, Q: Pointer; Beginp: = LOC (L, I); new (q); q ^ .data: = x; Q ^ .next : = P ^ .next; p ^ .next: = q; inc (L.LEN);

3. Single-link table deletion

Procedure delete (LINKLIST; I: Integer); var P, Q: Pointer; Beginp: = LOC (L, I-1); Q: = P ^ .next; p ^ .Next: = Q ^ .next; Dispose (q); DEC (L.LEN);

4. Insert operation of the double-linked list (insertion of new node Q)

P: = LOC (L, I); New (q); q ^ .data: = x; q ^ .pre: = p; q ^ .Next: = P ^ .next; p ^ .next: = q; Q ^ .next ^ .pre: = = q;

5. Double-linked list of deletions

P: = LOC (L, I); {P is the node to delete} p ^ .pre ^ .next: = p ^ .next; p ^ .next ^ .pre: = p ^ .pre; dispose (P );


New Post(0)