Pagini recente » Statistici Udroiu Ruben (UdroiuRuben) | Cod sursa (job #2107931) | Cod sursa (job #1639953) | Cod sursa (job #2714828) | Cod sursa (job #1180092)
Program bellmanford;
type lista = ^celula;
celula = record
nod : longint;
cost : longint;
pred : lista;
end;
const INF=200000000;
var Lda : array [1..300005] of lista;
r :lista;
Coada,D,viz: array [1..1000051] of longint;
n,i,a,b,c,v,aux,p,beg,sf,k: longint;
b1,b2 : array[0..1 shl 17] of char;
procedure qsort ( left, right : longint ) ;
var i,j, pivot,aux : longint;
begin
i:=left; j:=right; pivot:=D[(right+left) div 2];
repeat
while D[i]<pivot do i:=i+1;
while D[j]>pivot do j:=j-1;
if i<=j then begin
aux:=D[i];
D[i]:=D[j];
D[j]:=aux;
i:=i+1;
j:=j-1;
end;
until i>j;
if i<right then qsort(i,right);
if j>left then qsort(left,j);
end;
begin
assign(input,'darb.in'); settextbuf(input,b1); reset(input);
assign(output,'darb.out'); settextbuf(output,b2);rewrite(output);
readln(n); aux:=n; k:=0;
for i:=1 to n do viz[i]:=0;
while not seekeof do begin
readln(a,b);
new(r);
r^.nod:=b;
r^.cost:=1;
r^.pred:=lda[a];
lda[a]:=r;
new(r);
r^.nod:=a;
r^.cost:=1;
r^.pred:=lda[b];
lda[b]:=r;
end;
for i:=2 to n do D[i]:=INF;
coada[1]:=1; beg:=1; sf:=1;
while (beg<=sf) and (k=0) do begin
v:=coada[beg];
r:=lda[v];
while r<>nil do begin
if D[v]+r^.cost<D[r^.nod] then begin
D[r^.nod]:=D[v]+r^.cost;
sf:=sf+1;
coada[sf]:=r^.nod;
end;
r:=r^.pred;
end;
beg:=beg+1;
end;
qsort(1,n);
writeln(D[n]+D[n-1]+1);
close(input);
close(output);
end.