DINIC网络流+当前弧优化

时间:2022-09-06 22:01:05

深搜网络流+当前弧优化

 const
inf=;
type
rec=record
s,e,w,next:longint;
end;
var
b,bb,d,q:array[-..] of longint;
a:array[-..] of rec;
n,m,i,j,k,l,st,ed,ww,top,ans,nn,dd,x:longint;
function min(aa,bb:longint):longint;
begin
if aa<bb then exit(aa);exit(bb);
end;
procedure add(st,ed,ww:longint);
begin
inc(top);
a[top].s:=st;
a[top].e:=ed;
a[top].w:=ww;
a[top].next:=b[st];
b[st]:=top;
end;
function addflow(p,maxflow:longint):longint;
var
o,u:longint;
begin
if(p=ed)or(maxflow=)then exit(maxflow);
d[p]:=;
addflow:=; u:=bb[p];
while u> do
begin
if(a[u].w>)and(d[a[u].e]=)then
begin
o:=addflow(a[u].e,min(maxflow,a[u].w));
if o> then
begin
dec(a[u].w,o); if a[u].w> then bb[p]:=u;
inc(a[u xor ].w,o);
dec(maxflow,o); inc(addflow,o);
if maxflow= then break;
end;
end;
u:=a[u].next;
end;
end;
function network:longint;
var i,last:longint;
begin
network:=; last:=;
while true do
begin
for i:=st to ed do begin bb[i]:=b[i]; d[i]:=; end;
inc(network,addflow(st,inf));
if network=last then exit;
last:=network;
end;
end;
begin
readln(n,m);
top:=; build;
writeln(network);
end.

DINIC网络流+当前弧优化

 const
inf=;
type
rec=record
s,e,w,next:longint;
end;
var
b,bb,d,q:array[-..] of longint;
a:array[-..] of rec;
n,m,i,j,k,l,st,ed,ww,top,ans,nn,dd,x:longint;
function min(aa,bb:longint):longint;
begin
if aa<bb then exit(aa);exit(bb);
end;
procedure add(st,ed,ww:longint);
begin
inc(top);
a[top].s:=st;
a[top].e:=ed;
a[top].w:=ww;
a[top].next:=b[st];
b[st]:=top;
end;
function bfs:boolean;
var head,tail,x,u,i:longint;
y:rec;
begin
for i:=st to ed do d[i]:=-;
tail:=; head:=; d[st]:=; q[]:=st;
while head<tail do
begin
inc(head); x:=q[head];
u:=b[x];
while u> do
begin
y:=a[u];
if(d[y.e]=-)and(y.w>)then
begin
d[y.e]:=d[x]+;
inc(tail); q[tail]:=y.e;
end;
u:=y.next;
end;
end;
if d[ed]=- then exit(false);
exit(true);
end;
function addflow(p,maxflow:longint):longint;
var
o,u:longint;
begin
if(p=ed)or(maxflow=)then exit(maxflow);
addflow:=; u:=bb[p];
while u> do
begin
if(d[a[u].e]=d[p]+)and(a[u].w>)then
begin
o:=addflow(a[u].e,min(maxflow,a[u].w));
if o> then
begin
dec(a[u].w,o); if a[u].w> then bb[p]:=u;
inc(a[u xor ].w,o);
dec(maxflow,o); inc(addflow,o);
if maxflow= then break;
end;
end;
u:=a[u].next;
end;
if addflow= then d[p]:=-;
end;
function network:longint;
var i:longint;
begin
network:=;
while bfs do
begin
for i:=st to ed do bb[i]:=b[i];
inc(network,addflow(st,inf));
end;
end;
begin
readln(n,m);
top:=; build;
writeln(network);
end.

PASCAL

 #include<bits/stdc++.h>
using namespace std;
const int INF=;
int n,m,ss,tt,flow,l,r,lb[];
int c[],c2[],b[][],dis[];
void adedg(int x,int y,int z)
{
m++; b[m][]=z; b[m][]=y; b[m][]=c[x]; c[x]=m;
m++; b[m][]=; b[m][]=x; b[m][]=c[y]; c[y]=m;
}
bool bfs()
{
for(int i=ss;i<=tt;i++)dis[i]=-; dis[ss]=;
l=; r=; lb[r]=ss;
while(l<r)
{
l++; int x=lb[l];
for(int i=c[x];i;i=b[i][])
if((dis[b[i][]]==-)and(b[i][]>))
{ dis[b[i][]]=dis[x]+; r++; lb[r]=b[i][]; }
}
if(dis[tt]!=-)return ;
return ;
}
int work(int x,int ma)
{
if((ma==)or(x==tt))return ma;
int add=;
while(c2[x])
{
int i=c2[x];
if(dis[b[i][]]==dis[x]+)
{
int l=work(b[i][],min(b[i][],ma)); add+=l; ma-=l;
b[i][]-=l; b[i^][]+=l;
if(ma==)return add;
}
c2[x]=b[c2[x]][];
}
return add;
}
int main()
{
m=; build();
flow=;
while(bfs())
{
for(int i=ss;i<=tt;i++)c2[i]=c[i];
flow+=work(ss,INF);
}
printf("%d\n",flow);
}

C++

DINIC网络流+当前弧优化的更多相关文章

  1. &lbrack;Poj2112&rsqb;&lbrack;USACO2003 US OPEN&rsqb; Optimal Milking &lbrack;网络流,最大流&rsqb;&lbrack;Dinic&plus;当前弧优化&rsqb;

    题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...

  2. P3376 网络流-最大流模板题(Dinic&plus;当前弧优化)

    (点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...

  3. 网络流--最大流--Dinic模板矩阵版(当前弧优化&plus;非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...

  4. 【最大流之Dinic算法】POJ1273 【 &amp&semi; 当前弧优化 &amp&semi; 】

    总评一句:Dinic算法的基本思想比较好理解,就是它的当前弧优化的思想,网上的资料也不多,所以对于当前弧的优化,我还是费了很大的功夫的,现在也一知半解,索性就写一篇博客,来发现自己哪里的算法思想还没理 ...

  5. 最大流Dinic算法的一些优化 &lbrack;网络流&rsqb;&lbrack;最大流&rsqb;

    明天省夏要讲网络流啦!晚上翻出自己的模板发现是蓝书模板QwQ..拿出以前的提交代码(AC过的?) 曾经的提交记录 在luogu上重新提交一遍,结果gg...OVO 没有去除多余的inline 去除了多 ...

  6. 最大流当前弧优化Dinic模板

    最大流模板: 普通最大流 无向图限制:将无向图的边拆成2条方向相反的边 无源汇点有最小流限制的最大流:理解为水管流量形成循环,每根水管有流量限制,并且流入量等于流出量 有源汇点的最小流限制的最大流 顶 ...

  7. 最大流当前弧优化Dinic分层模板

    最大流模板: 普通最大流 无向图限制:将无向图的边拆成2条方向相反的有向边 顶点有流量限制:拆成2个点,连接一条容量为点容量限制的边 无源汇点有最小流限制的最大流:理解为水管流量形成循环 有源汇点的最 ...

  8. ARC085E(最小割规划【最大流】,Dinic当前弧优化)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...

  9. 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

随机推荐

  1. 怎么创建一个Database快照

    How to create the database Snapshot: use mastergoif not exists (select * from sys.databases where na ...

  2. NOIP2015 运输计划(bzoj4326)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 886  Solved: 574[Submit][Status] ...

  3. 在单机Hadoop上面增加Slave

    之前的文章已经介绍了搭建单机Hadoop, HBase, Hive, Spark的方式:link 现在希望在单机的基础上,加一个slave. 首先需要加上信任关系,加信任关系的方式,见前一篇文章:li ...

  4. 7&period;微软AJAX的解决方案

    Asp.net中内置的简化AJAX开发的控件UpdatePanel非常方便,但是它会带了许多viewstate,所以高手们并不喜欢它.但它开发在内网应用时可以考滤 放入ScriptManager,将要 ...

  5. 表单控件之select

    一.表单控件之表单 1.依次获取表单里的所有控件: for (i = 0; i < document.getElementById("formName").length; i ...

  6. HttpClient连接池抛出大量ConnectionPoolTimeoutException&colon; Timeout waiting for connection异常排查

    转自: http://blog.csdn.net/shootyou/article/details/6615051 今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎 ...

  7. django开发简易博客(一)

    这几篇博客是根据GoodSpeed的博客该写的,看了他的博客收获很大,但是他的博客从第三篇开始,条理很不清晰,加之又是几年之前写的,编写环境发生很大改变,所以对他的博客进行了一个整理,加入了一些自己的 ...

  8. Kill 进程

      动态杀各种进程,谨慎操作:事例 status='sleeping'   --AUTHOR      KiNg --DATE        2016-05-30 DECLARE @SPID INT ...

  9. A SimpleDataStore

    import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; impor ...

  10. BitConverter&period;GetBytes&lpar;int&rpar;和BitConverter&period;ToString 方法 &lpar;Byte&lbrack;&rsqb;&rpar;

    BitConverter.ToString 方法 (Byte[]) 网址:https://msdn.microsoft.com/zh-cn/library/3a733s97(v=vs.110).asp ...