Pagini recente » Cod sursa (job #1761024) | Cod sursa (job #1680377) | Cod sursa (job #1736457) | Cod sursa (job #2058477) | Cod sursa (job #256535)
Cod sursa(job #256535)
type pnod=^nod;
nod=record
cost:integer;
info:word;
urm:pnod;
end;
var v:array[1..50001]of pnod;
d:array[1..50001]of longint;
next:array[1..200001]of word;
q:pnod;
f,g:text;
m,s,i:longint;
n,p,u,poz:word;
procedure init(x,y,c:word);
var q:pnod;
begin
new(q);
q^.cost:=c;q^.info:=y;
if v[x]=nil then begin
q^.urm:=nil;
v[x]:=q;
end
else begin
q^.urm:=v[x];
v[x]:=q;
end;end;
begin
assign(f,'dijkstra.in');reset(f);
assign(g,'dijkstra.out');rewrite(g);
readln(f,n,m);
for i:=1 to m do begin
readln(f,p,u,s);
init(p,u,s);
init(u,p,s);
end;
for i:=2 to n do d[i]:=maxlongint;
p:=1;u:=1;next[p]:=1;
while p<=u do begin
q:=v[next[p]];
while q<>nil do begin
s:=d[next[p]]+q^.cost;
poz:=q^.info;
if s<d[poz] then begin
if d[poz]=maxlongint then begin
d[poz]:=s;
inc(u);
next[u]:=poz;
end
else d[poz]:=s;
end;
q:=q^.urm;
end;
inc(p);
end;
for i:=2 to n do if d[i]<>maxlongint then write(g,d[i],' ')
else write(g,'0 ');
close(g);
end.