-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRAFAS3.PAS
61 lines (56 loc) · 1.41 KB
/
GRAFAS3.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
{ MK 2001 }
{ Dijkstros algoritmas }
program grafas;
var
f : text;
pg1, pg2, pg3, ck,
sk, { virsuniu skaicius }
maz, { atstumas iki sios virsunes maziausias }
v1, v2 : shortint; { pradine ir galutine virsunes }
g : array [1 .. 100, 1 .. 100] of shortint;
griz, { is kurios i ta virs ateita }
ats : array [1 .. 100] of shortint; { atstumai tarp virsuniu }
aib : set of byte;
begin
assign (f, 'graf3.dat');
reset (f);
readln (f, sk, v1, v2);
while not eof (f) do
begin
readln (f, pg1, pg2, pg3);
g [pg1, pg2] := pg3;
g [pg2, pg1] := pg3;
end;
close (f);
for ck := 1 to sk do
ats [ck] := -1;
ats [v1] := 0;
aib := [1 .. sk];
while v2 in aib do
begin
ck := 1;
while not (ck in aib) do
inc (ck);
maz := ck;
{ randame asciausiai esancia nenagrineta virsune }
for ck := 1 to sk do
if (ck in aib) then
if (ats [ck] < ats [maz]) and (ats [ck] <> -1) then maz := ck;
aib := aib - [maz];
{ randam kelius i artimiausias nuo maz virs ir irasom ilgius }
for ck := 1 to sk do
if (ck in aib) and (g [maz, ck] <> 0) then
if (ats [ck] = -1) or ((ats [maz] + griz [ck]) < ats [ck]) then
begin
ats [ck] := ats [maz] + g [maz, ck];
griz [ck] := maz;
end;
end;
pg1 := v2;
while v1 <> pg1 do
begin
write (pg1, ' ');
pg1 := griz [pg1];
end;
writeln (v1)
end.