Air Raid(POJ1422)

本文深入解析了匈牙利算法的实现细节,并通过POJ1422实例进行详细讲解,帮助读者理解该算法在求解最大匹配问题中的应用。

匈牙利算法的裸题……

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.



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值