Cod sursa(job #256683)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 11 februarie 2009 23:50:56
Problema Algoritmul lui Dijkstra Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.81 kb
const nmax=50002;
const mmax=250002;
const inf=2000000000;
type muchie=record
        x,y,cost:longint;
        end;
var g:array[1..mmax]of muchie;
d:array[1..nmax]of longint;
i,x,y,c,m,n:longint;
ok:boolean;
f,h:text;
begin
assign(f,'dijkstra.in');
reset(f);
assign(h,'dijkstra.out');
rewrite(h);
read(f,n,m);
for i:=1 to m do
   begin
     read(f,x,y,c);
     g[i].x:=x;
     g[i].y:=y;
     g[i].cost:=c;
     if x=1 then d[y]:=c;
   end;
for i:=2 to n do
   if d[i]=0 then d[i]:=inf;
repeat
   ok:=true;
   for i:=1 to m do
      if d[g[i].y]>(d[g[i].x]+g[i].cost) then
             begin
               d[g[i].y]:=d[g[i].x]+g[i].cost;
               ok:=false;
             end;
   until ok;
for i:=2 to n do
if d[i]=inf then write(h,'0 ')
       else write(h,d[i],' ');
close(h);
end.