-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.pas
65 lines (55 loc) · 1.21 KB
/
matrix.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ 15.2 in Introduction to Algorithms }
Program Matrix_chain_order;
type
Tmas = array [0 .. 100] of integer;
var
ats : longint;
viso : Integer;
mas : TMas;
procedure nuskaitymas (var viso : integer; var mas : Tmas);
var
f : Text;
ck : Integer;
begin
assign (f, 'matrix.in');
reset (f);
readln (f, viso);
for ck := 0 to viso do
read (f, mas [ck]);
close (f)
end;
function rask (viso : Integer; var mas : TMas) : longint;
var
a : array [1 .. 100, 1 .. 100] of longint;
pg : longint;
ckILG, pr, pb, vid : Integer;
begin
for pr := 1 to viso do { ckILG := 1 }
a [pr, pr] := 0;
for ckILG := 2 to viso do
for pr := 1 to viso - ckILG + 1 do
begin
pb := pr + ckILG - 1;
a [pr, pb] := maxint;
for vid := pr to pb - 1 do
begin
pg := a [pr, vid] + a [vid + 1, pb] + mas [pr - 1] * mas [vid] * mas [pb];
if pg < a [pr, pb] then a [pr, pb] := pg
end
end;
rask := a [1, viso]
end;
procedure rasymas (ats : longint);
var
f : text;
begin
assign (f, 'matrix.out');
rewrite (f);
writeln (f, ats);
close (f)
end;
begin
nuskaitymas (viso, mas);
ats := rask (viso, mas);
rasymas (ats)
end.