深搜网络流+当前弧优化

 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. [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]

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

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

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

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

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

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

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

  5. 最大流Dinic算法的一些优化 [网络流][最大流]

    明天省夏要讲网络流啦!晚上翻出自己的模板发现是蓝书模板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. LBS上传到百度地图

    准备: 第一步:注册百度地图开发者账号 第二步:使用开发者账号注册相对应秘钥 在LBS后台添加所需要上传的字段 1,添加WebClientHelper帮助类 2,LBS帮助类 private stri ...

  2. 【代码笔记】iOS-翻书效果的实现

    代码: RootViewController.m #import "RootViewController.h" @interface RootViewController () @ ...

  3. Oc中的数组

    ========================== 数组 ========================== 一.认识数组 oc中可以把NSObject对象的子类放到数组这个集合中,但是int.f ...

  4. Android IPC机制之ContentProvider

    ContentProvider:即内容提供者,用来管理数据,并对外暴露一个uri,外部可以通过uri和数据建立联系并获取或操作数据: 服务端:1.首先创建一个数据库类,并创建一个表:2.创建一个Con ...

  5. 学习Coding-iOS开源项目日志(三)

    继续前两篇,接着本第三篇<学习Coding-iOS开源项目日志(三)>讲解Coding-iOS开源项目. 前 言:作为初级程序员,想要提高自己的水平,其中一个有效的学习方法就是学习别人好的 ...

  6. php设计模式 适配器模式

    适配器模式,可以将截然不同的函数接口封装成统一的API: 应用举例,PHP的数据库操作有Mysql.Mysqli.pdo三种,可以用适配器模式统一成一致,类似的场景还有cache适配器,将memcac ...

  7. C#复习③

    C#复习③ 2016年6月16日 11:13 Main Declaration & Statement 声明和语句 1.一个程序包含的声明空间有哪些? Namespace : declarat ...

  8. 给现有MVC 项目添加 WebAPI

    1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...

  9. MongoDB 常用故障排查工具

    1.profile profiling levels: 0,关闭profile:1,只抓取slow查询:2,抓取所有数据. 启动profile并且设置Profile级别: 可以通过mongo shel ...

  10. 说一下output子句

    Output子句日常灰常有用,而且用的地方也挺多,但是确好多时候被我们忽视,今天我就也简单扫盲一下这个语句的用法. Output子句 返回受 INSERT.UPDATE.DELETE 或 MERGE ...