匈牙利算法
http://unbelievable.ycool.com/post.696794.html
匈牙利算法真是一个不错的东西啊........
只要是求2分图匹配...用它准没错
下面是标准程序.....(一定要狠狠的背下来.....理解...然后运用)
g[i,j] ------ 这个就是 X 中的 i 是否与Y集合中的 j 有边连接 match[I] ------ Y集合中的 i 元素匹配 X 中的某个元素 ck[I] ------ 标记Y集合中的 i 元素是否被检测到过 (这个变量在检测过1遍后就不要再回溯的时候 赋回原来的值了...具体为什么还是朦朦胧胧的没搞得太清楚 听 Helin 大牛说是因为下一次你在匹配这个点时...上一次一定把这个点能匹配的情况都考虑过了.....) program hungary; const maxn=100; ouf='output.txt'; inf='input.txt'; var g:array [1..maxn,1..maxn] of boolean; ck:array [1..maxn] of boolean; match:array [1..maxn] of longint; a,b,ans:longint; procedure init; var x,y,n,i:longint; begin fillchar(g,sizeof(g),0); assign(input,inf); reset(input); readln(a,b,n); for i:=1 to n do begin readln(x,y); g[x,y]:=true; end; close(input); end; function search(k:longint):boolean; var i,t:longint; begin search:=true; for i:=1 to b do if g[k,i] and ck then begin ck:=false; t:=match; match:=k; if (t=0) or search(t) then exit; match:=t; end; search:=false; end; procedure main; var i:longint; begin ans:=0; for i:=1 to a do begin fillchar(ck,sizeof(ck),1); if search(i) then inc(ans); end; end; procedure out; begin assign(output,ouf);rewrite(output); writeln(ans); close(output); end; begin init; main; out; end. |

听 Helin 大牛说是因为下一次你在匹配这个点时...上一次一定把这个点能匹配的情况都考虑过了.....)
浙公网安备 33010602011771号