算法模板——sap网络最大流 2(非递归+邻接表)
2024-10-14 12:25:39
实现功能:同最大流 1
这里面主要是把前面的邻接矩阵改成了邻接表,相比之下速度大大提高——本人实测,当M=1000000 N=10000 时,暂且不考虑邻接矩阵会不会MLE,新的程序速度快了很多倍(我们家这个很弱的电脑上耗时0.3s);而当M=300000 N=10000时,优势更加明显(几乎是秒出),别的没了,尤其当遇到稀疏图的时候这样子是大大划算的!!!
type
point=^node;
node=record
g,w:longint;
next:point;
end; var
i,j,k,l,m,n,tmp,ans,aug,mi,s,t:longint;
di,a:array[..] of point;
pre,his,dis,vh:array[..] of longint;
flag:boolean;p,jl:point;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end;
function add(x,y,z:longint):longint;inline;
var p:point;
begin
new(p);p^.w:=z;p^.g:=y;
p^.next:=a[x];a[x]:=p;
end;
procedure op(x,y,z:longint);inline;
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if (p^.g=y) and ((p^.w+z)>=) then
begin
p^.w:=p^.w+z;
break;
end;
p:=p^.next;
end;
end;
begin
readln(n,m,s,t);
for i:= to n do a[i]:=nil;
for i:= to m do
begin
readln(j,k,l);
add(j,k,l);add(k,j,);
end;
for i:= to n do di[i]:=a[i];
fillchar(dis,sizeof(dis),);
fillchar(pre,sizeof(pre),);
fillchar(his,sizeof(his),);
fillchar(vh,sizeof(vh),);
i:=s;vh[]:=n;ans:=;aug:=maxlongint;
while dis[s]<n do
begin
flag:=false;his[i]:=aug;
p:=a[i];
while p<>nil do
begin
if (p^.w>) and (dis[i]=(dis[p^.g]+)) then
begin
aug:=min(aug,p^.w);
pre[p^.g]:=i;di[i]:=p;
flag:=true;i:=p^.g;
if i=t then
begin
ans:=ans+aug;
while i<>s do
begin
tmp:=i;
i:=pre[i];
op(i,tmp,-aug);
op(tmp,i,aug);
end;
aug:=maxlongint;
end;
break;
end;
p:=p^.next;
end;
if flag then continue;
jl:=nil;mi:=n-;
p:=a[i];
while p<>nil do
begin
if (p^.w>) and (dis[p^.g]<mi) then
begin
jl:=p;mi:=dis[p^.g];
end;
p:=p^.next;
end;
di[i]:=jl;
dec(vh[dis[i]]);
if vh[dis[i]]= then break;
dis[i]:=mi+;
inc(vh[dis[i]]);
if i<>s then
begin
i:=pre[i];
aug:=his[i];
end;
end;
writeln(ans);
end.
最新文章
- HTML中块级元素与行内元素
- CSS的一些零碎总结
- Android EventBus实战 没听过你就out了
- 挑战程序2.1.4 穷竭搜索>;>;深度优先搜索
- sublime2的快捷键
- 一个PHP写的简单webservice服务端+客户端
- 【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)
- hihocoder#1054 : 滑动解锁(深度优先搜索)
- OpenJudge/Poj 1207 The 3n + 1 problem
- uboot 连接脚本分析
- [C#] - 注入DLL
- awk的+=用法
- 学习ASP.NET Core Razor 编程系列十六——排序
- 从输入URL到页面加载的全过程
- 【bzoj 3495】PA2010 Riddle
- django——中间件
- io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket.server.WsServerContainer
- jQuery事件学习
- P2707 Facer帮父亲
- [转] - spark推荐 - 从50多分钟到3分钟的优化
热门文章
- NamingException with message: Name [spring.liveBeansView.mbeanDomain]
- weblogic 集群部署时上传jsp不更新问题
- 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现
- 访问 Neutron 外部网络 - 每天5分钟玩转 OpenStack(143)
- Eclipse TypeScript 安装
- Hadoop权威指南:FSDataInputStream对象
- 小学生之Hibernate插入数据修改数据使用数据库默认值的实现
- spring MVC cors跨域实现源码解析
- JavaScript从作用域到闭包
- Eclipse 报java.lang.UnsupportedClassVersionError: (";yourclass";) bad major version at offset=6