Basic algorithm (1) (PASCAL) ZZ

zhaozj2021-02-16  102

Basic algorithm

1. Number of algorithms

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;

Seeking two minimum common multiple

Function LCM (A, B: Integer): Integer;

Begin

IF a

LCM: = a;

While LCM MOD B> 0 DO INC (LCM, A);

END;

Justice

A. Decree a number in a small range 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;

END;

B. Judging whether the number within the longint range is the number of prime numbers (including the number of prime tables within 500,000):

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 <5000 do begin

IF P [i] THEN 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 5000 do

IF P [i] THEN Begin

INC (L); Pr [L]: = I;

END;

End; {getprime}

Function prime (x: longint): integer;

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}

2.

3.

4. Ask the minimum spanning tree

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] 0) THEN 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]

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;

{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;

5. Shortest 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 {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]

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 discussion point of J to J's shortest path }

Fork: = 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]

a [i, j]: = a [i, k] a [k, j];

P [I, J]: = P [k, j];

END;

END;

C. Dijkstra algorithm:

Similar reference numerals, essence is a greedy algorithm.

VAR

A: array [1..maxn, 1..maxn] of integer;

B, pre: array [1..maxn] of integer; {pre [i] refers to the front disseval 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 {Add a nearest node close to 1 episodes per loop 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]

u: = i; min: = d [i];

END;

IF u <> 0 THEN Begin

Mark [u]: = true;

For i: = 1 to n DO

IF (Not Mark [i]) and (a [u, i] d [u]

D [i]: = a [u, i] d [u];

Pre [i]: = u;

END;

END;

Until u = 0;

END;

D. Transfer closure of the calculation map

PROCEDURE longlink;

VAR

T: array [1..maxn, 1..maxn] of boolean;

Begin

Fillchar (T, SizeOf (t), false;

Fork: = 1 to n DO

For i: = 1 to n DO

For j: = 1 to n DO T [i, j]: = T [i, j] or (t [i, k] and t [k, j]);

END;

6.0-1 backpack problem (some backpack issues can have a greed method: calculate 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

There is 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

IF V- (s [n] -s [k-1])> = BEST THEN EXIT; {s [n] is the weight and} of the previous N items.

IF K <= n Then Begin

IF v> w [k] THEN Search (k 1, v-w [k]);

Search (k 1, v);

END;

END;

l dp

F [i, j] Select several pieces of the previous I to put it in a marker that is just J J. is a Boolean.

Implementation: Transformation of optimized problems into judgment issues

F [i, j] = f [i-1, j-w [i]] (w [i] <= j <= v) boundary: f [0 ,0]: = true.for i: = 1 to n DO

For j: = w [i] to v DO F [i, j]: = f [i-1, j-w [i]];

Optimization: The current state is only related to the previous stage state, which can be reduced to one dimension.

F [0]: = True;

For i: = 1 to n Do Begin

F1: = f;

For J: = W [i] to v DO

IF F [J-W [I]] THEN F1 [J]: = true;

F: = f1;

END;

B. The maximum value that can be placed.

F [i, j] =

C. Ask the number of applications that is full.

(2) Each backpack can use any time:

A. Ask for maximum weight.

The state transfer equation is

F [i, j] = max {f [i-w [j]

B. The maximum value that can be placed.

USACO 1.2 Score Inflation

Take 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 to answer this question) and a Si (answer this question) The resulting score), now choose a number of questions, the total number of points obtained by the total time of these questions, the maximum score obtained, and the maximum score.

* It is easy to think:

F [i, j] = max {f [i-k * w [j], j-1] k * v [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.

*optimization:

Begin

Fillchar (Problem, SizeOf (Problem), 0);

Assign (Input, 'Inflate.in');

RESET (INPUT);

Readln (M, N);

For i: = 1 to n DO

WITH Problem [i] DO

Readln (Point, TIME);

Close (INPUT);

Fillchar (f, sizeof (f), 0);

For i: = 1 to m DO

For j: = 1 to n DO

I-problem [J] .time> = 0 THEN

Begin

T: = problem [j] .point f [i-quobem [j] .time];

IF T> f [i] THEN F [I]: = T;

END;

Assign (Output, 'Inflate.out');

Rewrite (OUTPUT);

Writeln (f [m]);

Close (Output);

End.

C. Ask the number of applications that is full.

Ahoi2001 Problem2

Seeking natural number n Number of nature and the number of expressions.

Idea, generate the arrangement of the coefficients of each rigidity, test in one by one, this is a form of a law.

Procedure TRY (dep: integer);

Var i, J: integer;

Begin

Cal; {This process calculates the calculation result of the current coefficient, the result is the result}

IF now> n kiln exit; {scorch}

IF dep = L 1 Then Begin {Generates 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 2, 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;

Ideas 3: Can be solved using dynamic planning

USACo1.2 Money System

V Item, the backpack capacity is n, and the total number of flasks is found.

Transfer equation:

Procedure Update;

Var J, K: Integer;

Begin

C: = a;

For j: = 0 to n DO

IF a [J]> 0 THEN

Fork: = 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); {Reading 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 weight a value of the integer number of the first item is 1, as the initial value}

For i: = 2 to v DO

Begin

Read (now);

Update; {Dynamic Update}

END;

Writeln (a [n]);


New Post(0)