二分图最佳匹配(网络流)
来源:互联网 发布:数据库2000支持w7吗 编辑:程序博客网 时间:2024/05/19 17:51
var t,ii,i,j,n,cost,top,tail,min,tt,ttf:longint; pre,d:array[1..1000]of longint; b:array[1..10000]of longint; flag:array[1..1000]of boolean; f,c:array[1..200,1..200]of longint;begin read(t); for ii:=1 to t do begin fillchar(f,sizeof(f),0); fillchar(c,sizeof(c),0); read(n); for i:=2 to n+1 do for j:=n+2 to n+n+1 do begin read(c[i,j]); c[j,i]:=-c[i,j]; f[i,j]:=1; f[j,i]:=0; end; for i:=1 to n do for j:=1 to n do read(tt); for i:=2 to n+1 do begin c[1,i]:=0; c[i,1]:=0; f[1,i]:=1; f[i,1]:=0; end; for i:=n+2 to 2*n+1 do begin c[i,2*n+2]:=0; c[2*n+2,i]:=0; f[i,2*n+2]:=1; f[2*n+2,i]:=0; end; ttf:=0; cost:=0; while true do begin top:=1;tail:=1; b[top]:=1; fillchar(flag,sizeof(flag),true); for i:=1 to n+n+2 do d[i]:=maxlongint; fillchar(pre,sizeof(pre),0); flag[1]:=false; pre[1]:=-1; d[1]:=0; repeat for i:=1 to n+n+2 do if b[top]<>i then if f[b[top],i]>0 then if c[b[top],i]+d[b[top]]<d[i] then begin d[i]:=c[b[top],i]+d[b[top]]; pre[i]:=b[top]; if flag[i] then begin tail:=tail+1; b[tail]:=i; flag[i]:=false; end; end; flag[b[top]]:=true; top:=top+1; until top>tail; if d[n+n+2]=maxlongint then break; i:=n+n+2; min:=maxlongint; while pre[i]<>-1 do begin if min>f[pre[i],i] then min:=f[pre[i],i]; i:=pre[i]; end; i:=n+n+2; while pre[i]<>-1 do begin f[i,pre[i]]:=f[i,pre[i]]+min; f[pre[i],i]:=f[pre[i],i]-min; cost:=cost+c[pre[i],i]*min; i:=pre[i]; end; ttf:=ttf+min;// writeln(cost); end; writeln(cost*cost);// writeln(ttf); end; readln;readln;end.
加一个源点连向所有左边的点,加一个汇点连向所有右边的点,边的费用为0,流量为1。
中间的边费用为c,流量为1
0 0
- 二分图最佳匹配(网络流)
- [网络流24题] 18 分配问题(二分图最佳匹配,最小费用最大流)
- 二分图最佳匹配
- POJ1087(网络流,二分图匹配)
- [网络流] 二分图匹配
- 二分图最佳匹配 入门
- 二分图最佳匹配 题目
- 二分图的最佳匹配
- 二分图最佳完美匹配
- 二分图最佳完美匹配
- hdu2255 二分图最佳匹配
- 二分图 最佳匹配 最大权匹配
- POJ2195 Going Home 【最小费用流】+【二分图最佳匹配】
- 二分图的最佳匹配(KM 算法)
- nefu485分配问题(二分图的最佳匹配)
- LA - 4043 - Ants(二分图最佳完美匹配)
- hdu - 3488 - Tour(二分图最佳完美匹配)
- Ural 1076 Trash (二分图最佳完美匹配)
- swift基本语法04 字典
- Ubantu Gerrit 安装详解
- POJ 1861 Network (Kruskal求MST模板题)
- 【设计模式】单例设计模式的N中Java实现方法
- 【测试工具】Linux下查看指定进程的内存总和
- 二分图最佳匹配(网络流)
- 收获--大马、小马、非一句话木马、等相关hack工具、VPN
- pomelo 常见术语
- IE11特性之button
- A星寻路算法
- 提升ASP.NET性能的方法有哪些?
- jquery ajax使用实例
- Eclipse查找函数技巧
- pomelo 源码结构