匈牙利算法的裸题……
program POJ1422;
var
t,ii,n,m,ans:longint;
b:array [0..300,0..300] of boolean;
link:array [0..300] of longint;
use:array [0..300] of boolean;
function dfs(x:longint):boolean;
var
i:longint;
begin
if use[x] then exit(false);
use[x]:=true;
for i:=1 to n do
begin
if (b[x,i]) and ((link[i]=0) or (dfs(link[i]))) then
begin
link[i]:=x;
exit(true);
end;
end;
exit(false);
end;
procedure XYL;
var
i:longint;
begin
for i:=1 to n do
begin
fillchar(use,sizeof(use),false);
if dfs(i) then inc(ans);
end;
writeln(n-ans);
end;
procedure init;
var
i,x,y:longint;
begin
ans:=0;
fillchar(link,sizeof(link),0);
fillchar(b,sizeof(b),false);
readln(n,m);
for i:=1 to m do
begin
readln(x,y);
b[x,y]:=true;
end;
end;
begin
assign(input,'POJ1422.in'); reset(input);
assign(output,'POJ1422.out'); rewrite(output);
readln(t);
for ii:=1 to t do
begin
init;
XYL;
end;
close(input); close(output);
end.
本文深入解析了匈牙利算法的实现细节,并通过POJ1422实例进行详细讲解,帮助读者理解该算法在求解最大匹配问题中的应用。

被折叠的 条评论
为什么被折叠?



