You are on page 1of 8

Code minh họa

Câu 1.

Type mang=array[1..100]of integer;


Var n,i:byte;
dem:integer;
a:mang;
Procedure Qsort(Var a:mang;l,r:byte);
Var i,j:byte;mid,tg:integer;
Begin
i:=l;j:=r;mid:=a[(i+j)div 2];
Repeat
While a[i]>mid do inc(i);
While a[j]<mid do dec(j);
If i<=j then begin tg:=a[i];a[i]:=a[j];a[j]:=tg;inc(i);dec(j);end;
Until i>j;
If i<r then Qsort(a,i,r);
If l<j then Qsort(a,l,j);
End;
Begin
Readln(n);For i:=1 to n do read(a[i]);dem:=0;
Qsort(a,1,n);For i:=1 to n do if a[i]>(i-1)then dem:=dem+a[i]-(i-1) else break;
Write(dem);
End.

Câu 2.
const fi='Bai2.inp';
fo='Bai2.out';
nm=200;
x:array[1..4]of shortint=(1,0,-1,0);
y:array[1..4]of shortint=(0,1,0,-1);
var a:array[0..nm+1,0..nm+1]of shortint;
i,j,m,n,d,d1,d2,i1,i2,j1,j2,vti1,vtj1,vti2,vtj2:integer;
f:text;
procedure nhap1;
begin
fillchar(a,sizeof(a),$ff);
assign(f,fi); rewrite(f);
write('vao m,n='); readln(m,n);
writeln(f,m,' ',n);
for i:=1 to m do
begin
a[i,1]:=1;
a[i,n]:=1;
end;
for j:=1 to n do
begin
a[1,j]:=1;
a[m,j]:=1;
end;
for i:=2 to m-1 do
for j:=2 to n-1 do
a[i,j]:=0;
for i:=1 to m do
begin
for j:=1 to n do
write(f,a[i,j],' ');
writeln(f);
end;
close(f);
end;
procedure nhap;
begin
{ nhap1;}
fillchar(a,sizeof(a),$ff);
assign(f,fi); reset(f);
readln(f,m,n);
for i:=1 to m do
begin
for j:=1 to n do
read(f,a[i,j]);
readln(f);
end;
close(f);
end;
procedure loang(i,j:integer);
var k:integer;
begin
for k:=1 to 4 do
if a[i+x[k],j+y[k]]=1 then
begin
a[i+x[k],j+y[k]]:=2;
if i1>i+x[k] then i1:=i+x[k];
if i2<i+x[k] then i2:=i+x[k];
if j1>j+y[k] then j1:=j+y[k];
if j2<j+y[k] then j2:=j+y[k];
loang(i+x[k],j+y[k]);
end;
end;
procedure loang0(i,j:integer);
var k:integer;
begin
for k:=1 to 4 do
if (a[i+x[k],j+y[k]]=0) then
begin
a[i+x[k],j+y[k]]:=3;
if vti1>i+x[k] then vti1:=i+x[k];
if vti2<i+x[k] then vti2:=i+x[k];
if vtj1>j+y[k] then vtj1:=j+y[k];
if vtj2<j+y[k] then vtj2:=j+y[k];
loang0(i+x[k],j+y[k]);
end;
end;
function kt0(i1,j1,i2,j2:integer):boolean;
var i,j:integer;
begin
for i:=i1 to i2 do
for j:=j1 to j2 do
if a[i,j]<>3 then
begin
kt0:=false;
exit;
end;
kt0:=true;
end;
function ktcnr(i1,j1,i2,j2:integer):boolean;
var i,j,d,d0:integer;
begin
d:=0;d0:=0;
for i:=i1 to i2 do
for j:=j1 to j2 do
if a[i,j]=0 then
begin
vti1:=i; vtj1:=j;
vti2:=i; vtj2:=j;
a[i,j]:=3;
loang0(i,j);
inc(d0);
if kt0(vti1,vtj1,vti2,vtj2) then inc(d);
end;
if (d0=1)and(d=1) then ktcnr:=true
else ktcnr:=false;
end;
function ktcn(i1,j1,i2,j2:integer):boolean;
var i,j:integer;
begin
ktcn:=true;
for i:=i1 to i2 do
if a[i,j1]<>2 then begin ktcn:=false;exit; end;
for i:=i1 to i2 do
if a[i,j2]<>2 then begin ktcn:=false;exit; end;
for j:=j1 to j2 do
if a[i1,j]<>2 then begin ktcn:=false;exit; end;
for j:=j1 to j2 do
if a[i2,j]<>2 then begin ktcn:=false;exit; end;
end;
function ktcnd(i1,j1,i2,j2:integer):boolean;
var i,j:integer;
begin
ktcnd:=true;
for i:=i1 to i2 do
for j:=j1 to j2 do
if a[i,j]<>2 then begin ktcnd:=false;exit;end;
end;

procedure xuly;
begin
d:=0;d1:=0;d2:=0;
for i:=1 to m do
for j:=1 to n do
if a[i,j]=1 then
begin
i1:=i; j1:=j;
i2:=i; j2:=j;
a[i,j]:=2;
loang(i,j);
if ktcn(i1,j1,i2,j2) then
begin
inc(d);
if ktcnd(i1,j1,i2,j2) then inc(d1)
else
if ktcnr(i1,j1,i2,j2) then inc(d2);
end;
end;

end;
procedure print;
begin
assign(f,fo); rewrite(f);
writeln(f,d);
writeln(f,d1);
writeln(f,d2);
close(f);
end;
begin
nhap;
xuly;
print;
end.

Câu 3.
const
fi='paths.inp';
fo='paths.out';
base=1000000000+7;
oo=100000000;
type
Graph=^TEdge;
TEdge=record
u,v,w,ans:longint;
link:Graph;
end;
var
e:array[0..5000+5] of TEdge;
G:array[0..1500+5] of Graph;
a,path_to,path_from,d,heap,pos:array[0..1500+5] of longint;
nheap,n,m,i:longint;
procedure read_input;
var p:Graph;
u,v,w,i:longint;
begin
assign(input,fi);
reset(input);
readln(n,m);
for i:=1 to n do
G[i]:=nil;
for i:=1 to m do
begin
readln(u,v,w);
e[i].u:=u;
e[i].v:=v;
e[i].w:=w;
e[i].ans:=0;
e[i].link:=G[u];
G[u]:=@e[i];
end;
close(input);
end;
procedure write_output;
var i:longint;
begin
assign(output,fo);
rewrite(output);
for i:=1 to m do
writeln(e[i].ans);
close(output);
end;
procedure init;
var i:longint;
begin
for i:=1 to n do
pos[i]:=0;
nheap:=0;
end;
procedure upheap(i:longint);
var r,c:longint;
begin
c:=pos[i];
repeat
r:=c div 2;
if (r=0) or (d[i]>=d[heap[r]]) then break;
heap[c]:=heap[r];
pos[heap[c]]:=c;
c:=r;
until false;
heap[c]:=i;
pos[i]:=c;
end;
procedure downheap(i:longint);
var r,c:longint;
begin
r:=pos[i];
repeat
c:=r*2;
if (c<nheap) and (d[heap[c]]>d[heap[c+1]]) then
c:=c+1;
if (c>nheap) or (d[i]<=d[heap[c]]) then break;
heap[r]:=heap[c];
pos[heap[r]]:=r;
r:=c;
until false;
heap[r]:=i;
pos[i]:=r;
end;
function pop:longint;
var tam:longint;
begin
tam:=heap[1];
pos[tam]:=0;
heap[1]:=heap[nheap];
pos[heap[1]]:=1;
nheap:=nheap-1;
if nheap>0 then downheap(heap[1]);
pop:=tam;
end;
procedure push(i:longint);
begin
if pos[i]=0 then
begin
nheap:=nheap+1;
heap[nheap]:=i;
pos[i]:=nheap;
end;
upheap(i);
end;
procedure process(s:longint);
var nn,u,v,w,i:longint;
p:graph;
begin
for u:=0 to n do
begin
d[u]:=oo;
path_to[u]:=0;//so duong di ngan nhat den u
path_from[u]:=0;//so duong di ngan nhat di ra tu u
end;
init;
nn:=0;
d[s]:=0;
path_to[s]:=1;
push(s);//day s vao heap
repeat
u:=pop;//lay 1 phan tu ra tu heap;
nn:=nn+1;
a[nn]:=u;
p:=G[u];
while p<>nil do
begin
v:=p^.v;
w:=p^.w;
if d[u]+w<d[v] then
begin
d[v]:=d[u]+w;
path_to[v]:=path_to[u];
push(v);
end
else
if d[u]+w=d[v] then
path_to[v]:=(path_to[v]+path_to[u]) mod base;
p:=p^.link;
end;
until nheap=0;
//tinh path_from
for i:=nn downto 1 do
begin
u:=a[i];
path_from[u]:=1;
p:=G[u];
while p<>nil do
begin
v:=p^.v;
w:=p^.w;
if d[u]+w=d[v] then
begin
path_from[u]:=(path_from[u]+path_from[v]) mod base;
p^.ans:=((int64(path_to[u]) mod base)*(path_from[v] mod
base)+p^.ans) mod base;
end;
p:=p^.link;
end;
end;

end;
BEGIN
read_input;
for i:=1 to n do
process(i);
write_output;
END.

You might also like