Pagini recente » Cod sursa (job #1127852) | Cod sursa (job #514788) | Cod sursa (job #1768881) | Cod sursa (job #1912894) | Cod sursa (job #554712)
Cod sursa(job #554712)
uses crt;
type mat=array[1..2000,1..2000] of integer;
vek=array[1..5000] of integer;
var e,jr:^vek; x:mat; i,j,a,b,c,n,m,kcs,l,cs:integer; f:text;
procedure lol(var cs:integer);
var i,min,cs1:integer; t:boolean;
begin
min:=l; t:=false;
cs1:=0;
for i:=1 to n do
if (e^[i]<min)and(jr^[i]<>1) then begin min:=e^[i]; cs1:=i; end;
if cs1<>0 then jr^[cs1]:=1;
for i:=1 to n do
if jr^[i]=0 then t:=true;
if t and (cs1<>0) then begin
for i:=1 to n do begin
if x[cs1,i]+min<e^[i] then begin e^[i]:=x[cs1,i]+min; {os[i]:=cs1;} end;
end;
cs:=cs1;
lol(cs);
end;
end;
begin
assign(f,'dijkstra.in');
reset(f);
readln(f,n,m);
l:=0;
new(jr); new(e);
if m<>0 then begin
for i:=1 to m do begin
readln(f,a,b,c);
if c<>0 then begin x[a,b]:=c; x[b,a]:=c; end
else begin x[a,b]:=-1; x[b,a]:=-1; end;
l:=l+c;
end;
close(f);
l:=l+1;
kcs:=1;
for i:=1 to n do
for j:=1 to n do
if x[i,j]=0 then x[i,j]:=l
else if x[i,j]=-1 then x[i,j]:=0;
for i:=1 to n do
if i<>kcs then e^[i]:=x[kcs,i]
else if i=kcs then e^[i]:=0
else e^[i]:=l;
for i:=1 to n do
jr^[i]:=0;
jr^[kcs]:=1;
cs:=kcs;
{ for i:=1 to n do
os[i]:=kcs;
os[kcs]:=0; }
lol(cs);
end;
assign(f,'dijkstra.out');
rewrite(f);
for i:=2 to n do
if e^[i]<>l then write(f,e^[i],' ')
else write(f,0,' ');
close(f);
end.