Pagini recente » Cod sursa (job #3177339) | Cod sursa (job #2924674) | Cod sursa (job #1345600) | Cod sursa (job #609106) | Cod sursa (job #256551)
Cod sursa(job #256551)
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;
viz:array[1..50001]of boolean;
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;viz[p]:=true;
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
d[poz]:=s;
if viz[poz]=false then begin
inc(u);
next[u]:=poz;
viz[poz]:=true;
end;end;
q:=q^.urm;
end;
viz[next[p]]:=false;
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.