Professional Documents
Culture Documents
Problema Calutzi - Pascal
Problema Calutzi - Pascal
Problema Calutzi - Pascal
{$I-,R-,Q-}
Program CAI;
type cal=record
l,c:integer;
end;
var f:text;
pr,last,l,c,i,j,n,p,point,nel:integer;
a,flux,cap:array[0..51,0..51] of integer;
cost:array[0..51,0..51] of longint;
ln,cn,npa,lin,col:integer;
natac,poz:array[1..8] of cal;
costt,fm:longint;
coada:array[1..2500] of cal;
cod:array[1..10000] of integer;
dist:array[0..50] of longint;
incoada:array[0..100] of boolean;
tip,tata:array[0..500] of byte;
begin
assign(f,'cai.in');
reset(f);
readln(f,n,p);
readln(f,ln,cn);
lin:=ln;
col:=cn;
for i:=1 to p do
readln(f,poz[i].l,poz[i].c);
close(f);
npa:=0;
inc(npa);
natac[npa].l:=ln-2;
natac[npa].c:=cn-1;
end;
inc(npa);
natac[npa].l:=ln-1;
natac[npa].c:=cn-2;
end;
inc(npa);
natac[npa].l:=ln-2;
natac[npa].c:=cn+1;
end;
inc(npa);
natac[npa].l:=ln-1;
natac[npa].c:=cn+2;
end;
inc(npa);
natac[npa].l:=ln+1;
natac[npa].c:=cn-2;
end;
inc(npa);
natac[npa].l:=ln+1;
natac[npa].c:=cn+2;
end;
inc(npa);
natac[npa].l:=ln+2;
natac[npa].c:=cn-1;
end;
inc(npa);
natac[npa].l:=ln+2;
natac[npa].c:=cn+1;
end;
assign(f,'cai.out');
rewrite(f);
writeln(f,'imposibil');
close(f);
halt;
end;
for i:=0 to n+1 do
nel:=1;
point:=1;
coada[1].l:=poz[i].l;
coada[1].c:=poz[i].c;
for l:=1 to n do
a[poz[i].l,poz[i].c]:=0;
ln:=coada[point].l;
cn:=coada[point].c;
a[ln-2,cn-1]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln-2;
coada[nel].c:=cn-1;
end;
end;
if (ln-1>0) and (cn-2>0) then begin
a[ln-1,cn-2]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln-1;
coada[nel].c:=cn-2;
end;
end;
a[ln-2,cn+1]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln-2;
coada[nel].c:=cn+1;
end;
end;
a[ln-1,cn+2]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln-1;
coada[nel].c:=cn+2;
end;
end;
inc(nel);
coada[nel].l:=ln+1;
coada[nel].c:=cn-2;
end;
end;
a[ln+1,cn+2]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln+1;
coada[nel].c:=cn+2;
end;
end;
a[ln+2,cn-1]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln+2;
coada[nel].c:=cn-1;
end;
end;
a[ln+2,cn+1]:=a[ln,cn]+1;
inc(nel);
coada[nel].l:=ln+2;
coada[nel].c:=cn+1;
end;
end;
inc(point);
end;
cost[i,p+j]:=a[natac[j].l,natac[j].c];
cost[p+j,i]:=-cost[i,p+j];
end;
end;
last:=p+npa+1;
cap[i,j]:=0;
flux[i,j]:=0;
end;
cap[0,i]:=1;
cost[0,i]:=0;
cost[i,0]:=0;
end;
for j:=1 to p do
end;
cap[i,last]:=1;
cost[i,last]:=0;
cost[last,i]:=0;
end;
costt:=0;
fm:=0;
repeat
dist[i]:=30000;
incoada[i]:=false;
tata[i]:=255;
end;
point:=1;
nel:=1;
cod[1]:=0;
dist[0]:=0;
tata[0]:=0;
incoada[0]:=true;
if (dist[i]>dist[cod[point]]+cost[cod[point],i]) then
begin
if (cap[cod[point],i]-flux[cod[point],i]>0) or
then begin
dist[i]:=dist[cod[point]]+cost[cod[point],i];
tata[i]:=cod[point];
else tip[i]:=2;
inc(nel);
cod[nel]:=i;
incoada[i]:=true;
end;
end;
end;
incoada[cod[point]]:=false;
inc(point);
end;
pr:=last;
case tip[pr] of
1:flux[tata[pr],pr]:=flux[tata[pr],pr]+1;
2:flux[pr,tata[pr]]:=flux[pr,tata[pr]]-1;
end;
pr:=tata[pr];
end;
costt:=costt+dist[last];
fm:=fm+1;
end;
until dist[last]=30000;
writeln(f,costt);
close(f);
end.