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] 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] 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] 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 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 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] 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]);