program TT_Dijkstra;
uses crt;
const max=50;
var
n,s,t: integer;
chon:char;
truoc:array[1 max] of byte;
d :array[1 max] of integer;
a :array[1 max,1 max] of integer;
kt :array[1 max] of boolean;
(******************Nhap So Lieu***********************) procedure NhapSoLieu;
Var
f:text;
fname:string;
i,j:integer;
begin
write('nhap ten tep:') ; readln(fname);
assign(f,fname);
reset(f);
readln(f,n);
for i:=1 to n do
for j:=1 to n do read(f,a[i,j]);
close(f);
end;
(******************In So Lieu**************************) procedure Inketqua;
var i,j:integer;
begin
Write('DUONG DI NGAN NHAT TU',s,'DEN',t);
write(t,'<=');
i:=truoc[t];
while i<>s do
begin
write(i,'<=');
i:=truoc[i];
end;
writeln(s);
writeln('Do dai cua duong di la: ',d[t]);
end;
(*******************************************************) procedure Dijkstra;
var v,u,minp:integer;
begin
write('Tinh duong di tu s= ');
readln(s);
write(' den t=');
readln(t);
for v:=1 to n do
begin (*Khoi tao nhan*)
d[v]:=a[s,v];
truoc[v]:=s;
kt[v]:=false;
end;
truoc[s]:=0;
d[s]:=0;
kt[s]:=true;
while not kt[v] do (*Buoc lap*)
begin
{Tim u la dinh co nhan tam thoi nho nhat}
Trang 2minp:=maxint;
for v:=1 to n do
begin
u:=v;
minp:=d[v];
end;
kt[u]:=true;
if not kt[t] then
for v:=1 to n do
if (not kt[v]) and (d[u]+a[u,v]<d[v]) then begin
d[v]:=d[u]+a[u,v];
truoc[v]:=u;
end;
end;
end;
(********************************************************) BEGIN
clrscr;
Nhapsolieu;
dijkstra;
Inketqua;
readln;
END