Pagini recente » Cod sursa (job #1756179) | Cod sursa (job #2880276) | Cod sursa (job #169887) | Cod sursa (job #2728590) | Cod sursa (job #879219)
Cod sursa(job #879219)
program dijkstra;
const pinf=32767;
var f,g:text;
s,t:array[1..10000] of longint;
d:array[1..10000] of longint;
v:array[1..1000,1..1000] of integer;
i,l,c,q,n,j,m,min,poz:longint;
{procedure drum(i:integer);
begin
if t[i]<>0 then
drum(t[i]);
write(i,' ');
end;
}
begin
assign(f,'dijkstra.in');
reset(f);
read(f,n,m);
for i:=1 to n do
for j:=1 to n do
if i=j then
v[i,j]:=0
else
v[i,j]:=pinf;
for i:=1 to m do
begin
read(f,l,c,q);
v[l,c]:=q;
end;
close(f);
for i:=1 to n do
begin
s[i]:=0;
t[i]:=0;
end;
s[1]:=1;
for i:=1 to n do
begin
d[i]:=v[1,i];
if i<>1 then
if d[i]<pinf then
t[i]:=1;
end;
for i:=1 to n-1 do
begin
min:=pinf;
poz:=-1;
for j:=1 to n do
if s[j]=0 then
if d[j]<min then
begin
min:=d[j];
poz:=j;
end;
if poz<>-1 then
begin
s[poz]:=1;
for j:=1 to n do
if s[j]=0 then
if (d[j]>d[poz]+v[poz,j]) then
begin
d[j]:=d[poz]+v[poz,j];
t[j]:=poz;
end
end;
end;
assign(g,'dijkstra.out');
rewrite(g);
for i:=1 to n do
if i<>1 then
if t[i]<>0 then
begin
write(g,d[i],' ');
// drum(i);
end
else
write(g,0,' ');
close(g);
end.