Cod sursa(job #455829)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 14 mai 2010 11:54:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.73 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
readln(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
   ok:=false;
   d[g[i].y]:=d[g[i].x]+g[i].cost;
   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.