procedure addedge(u,v,cap:longint);
begin
sid[tot].u:=u;
sid[tot].v:=v;
sid[tot].cap:=cap;
sid[tot].next:=nd[u];
nd[u]:=tot;
inc(tot);
sid[tot].u:=v;
sid[tot].v:=u;
sid[tot].cap:=0;
sid[tot].next:=nd[v];
nd[v]:=tot;
inc(tot);
end;
 function bfs:boolean;
var
i,head,tail,u,p:longint;
begin
for i:=0 to n*m+1 do
dep[i]:=-1;
dep[0]:=0;
head:=1;tail:=1;dl[1]:=0;
while head<=tail do
begin
u:=dl[head];
p:=nd[dl[head]];
while p<>-1 do
begin
if (sid[p].cap>0) and (dep[sid[p].v]=-1) then
begin
dep[sid[p].v]:=dep[u]+1;
if sid[p].v=n*m+1 then exit(true);
inc(tail);
dl[tail]:=sid[p].v;
end;
p:=sid[p].next;
end;
inc(head);
end;
exit(false);
end; function dinic:longint;
var
top,i,u,mi,last:longint;
begin
dinic:=0;
while bfs do
begin
fillchar(stack,sizeof(stack),0);
top:=0;
for i:=0 to n*m+1 do cur[i]:=nd[i];
u:=0;
while true do
begin
if u=n*m+1 then
begin mi:=maxlongint;
for i:=1 to top do
begin
if sid[stack[i]].cap<mi then
begin
last:=i;
mi:=sid[stack[i]].cap;
end;
end; for i:=1 to top do
begin
dec(sid[stack[i]].cap,mi);
inc(sid[stack[i] xor 1].cap,mi);
end; top:=last-1;
dinic:=dinic+mi;
u:=sid[stack[last]].u;
end; while cur[u]<>-1 do
begin
if (dep[sid[cur[u]].v]=dep[u]+1) and (sid[cur[u]].cap<>0) then break
else cur[u]:=sid[cur[u]].next;
end; if cur[u]=-1 then
begin
if u=0 then break;
dep[u]:=-1;
u:=sid[stack[top]].u;
dec(top);
end else
begin
inc(top);
stack[top]:=cur[u];
u:=sid[cur[u]].v;
end;
end;
end;
end;
 

最大流=最小割=正点权和-最大闭合权图

其中求最大闭合权图时,源点向所有点权为正的点连容量为点权的边,所有点权为负的点向汇点连容量为点权绝对值的边

表示约束关系的边容量为无穷大,i前必须做j,i连向j

-----------------------------------------------------------------------------------------------

  void addedge2(int u,int v,int cap){
sid[cnt].next=nd[u];sid[cnt].des=v;sid[cnt].cap=cap;nd[u]=cnt;sid[cnt].fr=u;cnt++;
sid[cnt].next=nd[v];sid[cnt].des=u;sid[cnt].cap=;nd[v]=cnt;sid[cnt].fr=v;cnt++;
} int bfs(){
memset(dep,-,sizeof(dep)); int head=,tail=;
dep[sor]=;dl[head]=sor;
while (head<=tail){
for (int p=nd[dl[head]];p!=-;p=sid[p].next)
if ((dep[sid[p].des]==-)&&(sid[p].cap)) dep[sid[p].des]=dep[dl[head]]+,dl[++tail]=sid[p].des;
head++;
} if (dep[tar]==-) return();else return();
} int dinic(){
int maxflow=;
while (bfs()){
for (int i=;i<=tar;i++) cur[i]=nd[i]; int u=sor,top=;
while(){
if (u==tar){
int mi=1e9,last;
for (int i=;i<=top;i++)
if (sid[sta[i]].cap<mi)
{mi=sid[sta[i]].cap;last=i;} for (int i=;i<=top;i++)
sid[sta[i]].cap-=mi,sid[sta[i]^].cap+=mi;
u=sid[sta[last]].fr;cur[u]=sid[cur[u]].next;top=last-;
maxflow+=mi;
continue;
} while((cur[u]!=-)&&((sid[cur[u]].cap==)||(dep[sid[cur[u]].des]!=dep[u]+)))
cur[u]=sid[cur[u]].next; if (cur[u]!=-){sta[++top]=cur[u];u=sid[cur[u]].des;continue;}
else{
if (u==sor) break;
dep[u]=-;
u=sid[sta[top--]].fr;
continue;
}
}
}
return(maxflow);
}

dinic模板的更多相关文章

  1. hdu 1532 Dinic模板(小白书)

    hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...

  2. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  3. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  4. 洛谷P3376【模板】网络最大流  Dinic模板

    之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...

  5. 【网络流#3】hdu 1532 - Dinic模板题

    输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...

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

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

  7. 网络流--最大流dinic模板

    标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...

  8. HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. POJ1459Power Network(dinic模板)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25832   Accepted: 13481 D ...

随机推荐

  1. Using Confluent’s JDBC Connector without installing the entire platform

    转自:https://prefrontaldump.wordpress.com/2016/05/02/using-confluents-jdbc-connector-without-installin ...

  2. db2数组、函数

    一. 数组 在db2中,创建一个数组会在functions下生成两个对象:sys类型和用户类型的Array /*创建数组*/ ) array[]; /*删除数组*/ drop type arrName ...

  3. ARM体系结构

    工作模式_ufisaus USR(User) :正常程序的执行状态 FIQ(Fast interrupt) :用于高速数据传输和通道处理 IRQ(Interrupt) :通常的中断处理 SVC(Sup ...

  4. python关键字,运算符

    关键字: and且 or 或 not否 del import导入 from import的来源 whilewhile循环 for for循环 if elif else条件结构 break contin ...

  5. 《PHP开发APP接口》笔记

    PHP开发APP接口 [TOC] 课程地址 imooc PHP开发APP接口 学习要点 APP接口简介 封装通信接口方法 核心技术 APP接口实例 服务器端 -> 数据库|缓存 -> 调用 ...

  6. linux添加开机自启动脚本示例详解

    linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...

  7. /etc/fstab 参数详解及如何设置开机自动挂载

    某些时候当Linux系统下划分了新的分区后,需要将这些分区设置为开机自动挂载,否则,Linux是无法使用新建的分区的. /etc/fstab 文件负责配置Linux开机时自动挂载的分区. Window ...

  8. HowTo: Linux Server Change OR Setup The Timezone

    Method 1 #tzselect # select timezone e.g. Asia/Shanghai#echo 'Asia/Shanghai' > /etc/timezone # se ...

  9. 通过vmstat命令判断服务器瓶颈

    linux命令vmstat介绍 vmstat介绍 通过STATSPACK收集服务器信息,主要通过收集VMSTAT的信息来展现服务器状况.VMSTAT工具是最常见的UNIX监控工具,可以展现给定时 间间 ...

  10. ARM学习篇一 点亮LED

    要点亮LED,先决条件是什么,当然得有相应的硬件设施.板子的整个电路图比较大,我就直接取相关部分. 给发光二级管加上3.3v电压后,通过1k电阻,直接与S3C2440连接.至于为什么要加电阻,大家应该 ...