Cod sursa(job #67577)
Utilizator | Data | 25 iunie 2007 12:03:28 | |
---|---|---|---|
Problema | Obiective | Scor | 15 |
Compilator | fpc | Status | done |
Runda | preONI 2007, Runda Finala, Clasele 11-12 | Marime | 6.35 kb |
program obiective;
const
fin='obiective.in';
fout='obiective.out';
nmax=32002;
type
edge=^elem;
elem=record
x:integer;
urm:edge;
sens:boolean;
end;
vect=array[1..nmax] of integer;
v=^vect;
coada=^chestie;
chestie=record
urm:coada;
x:integer;
end;
var
cost:array[1..nmax] of v;
e:array[1..nmax] of edge;
ind:integer;
aux:edge;
viz:array[1..nmax] of boolean;
cap,ult,act,q:coada;
n,r,i,j,k,m,x,y:longint;
procedure insert(x,y:integer;ok:boolean);
var
aux:edge;
begin
new(aux);
aux^.x:=y;
aux^.urm:=e[x]^.urm;
aux^.sens:=ok;
e[x]^.urm:=aux;
end;
begin
assign(input,fin);
assign(output,fout);
reset(input);
rewrite(output);
readln(n,m);
for i:=1 to n do
begin
new(e[i]);
e[i]^.urm:=nil;
new(cost[i]);
end;
for i:=1 to m do
begin
readln(x,y);
insert(x,y,true);
insert(y,x,false);
end;
for r:=1 to n do
begin
fillchar(viz,sizeof(viz),false);
viz[r]:=true;
for i:=1 to n do
cost[r]^[i]:=maxint;
cost[r]^[r]:=0;
new(cap);cap^.x:=r;
new(ult);ult^.urm:=nil;cap^.urm:=ult;
act:=cap;
aux:=e[r]^.urm;
while aux<>nil do
begin
if aux^.x<r then
begin
if aux^.sens=true then
begin
if cost[r]^[act^.x]<cost[r]^[aux^.x] then
begin
if viz[aux^.x] then
cost[r]^[aux^.x]:=cost[r]^[act^.x]
else
begin
viz[aux^.x]:=true;
cost[r]^[aux^.x]:=cost[r]^[act^.x];
new(q);q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
for i:=1 to n do
if (i<>r)and(i<>aux^.x) then
if cost[aux^.x]^[i]+cost[r]^[aux^.x]>cost[r]^[i] then
begin
if viz[i]=true then
cost[r]^[i]:=cost[aux^.x]^[i]+cost[r]^[aux^.x]
else
begin
viz[i]:=true;
cost[r]^[i]:=cost[aux^.x]^[i]+cost[r]^[aux^.x];
new(q);
q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
end;
end
end
else
begin
if cost[r]^[act^.x]+1<cost[r]^[aux^.x] then
begin
if viz[aux^.x] then
cost[r]^[aux^.x]:=cost[r]^[act^.x]+1
else
begin
viz[aux^.x]:=true;
cost[r]^[aux^.x]:=cost[r]^[act^.x]+1;
new(q);q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
for i:=1 to n do
if (i<>r)and(i<>aux^.x) then
if cost[aux^.x]^[i]+cost[r]^[aux^.x]>cost[r]^[i] then
begin
if viz[i]=true then
cost[r]^[i]:=cost[aux^.x]^[i]+cost[r]^[aux^.x]
else
begin
viz[i]:=true;
cost[r]^[i]:=cost[aux^.x]^[i]+cost[r]^[aux^.x];
new(q);
q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
end;
end
end;
end;
aux:=aux^.urm;
end;
while act<>ult do
begin
aux:=e[act^.x]^.urm;
while aux<>nil do
begin
if aux^.sens=true then
begin
if cost[r]^[act^.x]<cost[r]^[aux^.x] then
begin
if viz[aux^.x] then
cost[r]^[aux^.x]:=cost[r]^[act^.x]
else
begin
viz[aux^.x]:=true;
cost[r]^[aux^.x]:=cost[r]^[act^.x];
new(q);q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
end
end
else
begin
if cost[r]^[act^.x]+1<cost[r]^[aux^.x] then
begin
if viz[aux^.x] then
cost[r]^[aux^.x]:=cost[r]^[act^.x]+1
else
begin
viz[aux^.x]:=true;
cost[r]^[aux^.x]:=cost[r]^[act^.x]+1;
new(q);q^.urm:=nil;
ult^.x:=aux^.x;
ult^.urm:=q;
ult:=q;
end;
end
end;
aux:=aux^.urm;
end;
q:=act;
viz[act^.x]:=false;
act:=act^.urm;
dispose(q);
end;
end;
readln(k);
for i:=1 to k do
begin
readln(x,y);
writeln(cost[x]^[y]);
end;
close(input);
close(output);
end.