songzi 2008-5-1 22:31
匈牙利算法参考程序
匈牙利算法参考程序const
maxn=100;
var
i,k,m,n: longint;
w: array[1..maxn,1..maxn] of boolean;
match: array[1..maxn] of integer;
look: array[1..maxn] of boolean;
procedure init;
var
x,y: longint;
begin
fillchar(w,sizeof(w),false);
fillchar(match,sizeof(match),0);
readln(n,m);
for i:=1 to m do begin
readln(x,y);
w[x,y]:=true;
end;
end;
function find(x:integer):boolean;
var
j: integer;
begin
for j:=1 to n do
if w[x,j] and look[j] then begin
look[j]:=false;
if (match[j]=0) or find(match[j]) then begin
match[j]:=x;
find:=true;
exit;
end;
end;
find:=false;
end;
procedure print;
begin
k:=0;
for i:=1 to n do begin
writeln(match, ' ',i);
if match<>0 then
inc(k);
end;
writeln(k);
end;
begin
assign(input, 'match.in ');
reset(input);
assign(output, 'match.out ');
rewrite(output);
init;
for i:=1 to n do begin
fillchar(look,sizeof(look),true);
find(i);
end;
print;
close(input);
close(output);
end.