网络流dinic实现总结

太羞耻了,搞了半天居然没发现自己写的不是dinic,直到被一道时限紧的题目卡掉才发现

 1 int dfs(int now,int flow,int sum)
 2 {
 3     if(now==n) return flow;
 4     for(int i=fir[now];i && (flow>sum);i=nex[i])
 5         if(d[to[i]]==d[now]+1 && flo[i])
 6             zl=dfs(to[i],min(flow,flo[i]),0),sum+=zl,flo[i]-=zl,flo[i^1]+=zl;
 7     if(sum==0) d[now]=0;
 8     return sum;
 9 }
10 bool bfs()
11 {
12     for(int i=1;i<=n;i++) d[i]=0;
13     for(h=1,t=1,l[1]=0,d[0]=1;h<=t;h++)
14         for(int i=fir[l[h]];i;i=nex[i])
15             if(!d[to[i]] && flo[i])
16                 l[++t]=to[i],d[l[t]]=d[l[h]]+1;
17     return d[n];
18 }

俗话说dinic=bfs+dfs,bfs和dfs各写9行真是和谐美妙啊

有几处地方保证了复杂度的优化:

1.在总流量达到限制时直接滚粗

2.如果从一个节点无法流到终点,那么就暂时无视这个点(直到重新标号)——一开始一直没写

在调用的时候可以直接写

1 for(ans=0;bfs();ans+=dfs(0,INF,0));

不要手贱在ans前面加int,又一次我搞半天都只能输出0,结果发现╮(╯▽╰)╭

——另外不是很理解网上把残余流量和流量分开写的方法,感觉有点冗余

未完待续

时间: 2024-10-11 10:45:49

网络流dinic实现总结的相关文章

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

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage

[网络流dinic]日常翻新

之前的排版简直辣眼睛,重写一遍好了 模板题是草地排水poj1273 网络流的基础思想就是瞎基本搜 但是搜要搜得有技巧,有特色 最简单的搜,无限深搜直到终点 稍微改进一下,宽搜先标号然后按层搜 再改进一下,把某些确定不再使用的点剔除 要点在于建立反向边给自己一个反悔的机会,用^1找到反向边 #include <stdio.h> #include <queue> #include <algorithm> #include <string.h> using nam

hdu2732 Leapin&#39; Lizards (网络流dinic)

D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasur

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

网络流Dinic(本篇介绍最大流)

前言:看到网上Dinic和ISAP的比较,多数人认为ISAP更快,不容易爆栈.当然,也有少数人认为,在多数情况下,Dinic比较稳定.我认为Dinic的思路比ISAP更简明,所以选择了Dinc算法 介绍:Dinic算法本身,自然是解决最大流(普通最大流,最大流最小割)的算法.通过处理,也可以解决二分图的最大匹配(下文介绍),最大权闭合图. 算法介绍:介绍Dinic之前,我们先介绍一下最大流.在最大流的题目中,图被称为"网络",每条边的边权被称作"流量",有一个起点(

网络流 dinic

1 struct Edge { 2 int v, w; 3 int next; 4 }edge[250*250]; 5 int head[50], tot; 6 void addedge(int u, int v, int w) { 7 edge[tot].v = v; 8 edge[tot].w = w; 9 edge[tot].next = head[u]; 10 head[u] = tot++; 11 } 12 int lvl[305]; 13 bool bfs(int s, int t)

POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321   Accepted: 8372 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offi

网络流dinic模板

#include <iostream> #include <cstdlib> #include <stdio.h> #include <map> #include <cstring> #include <math.h> #include <stdlib.h> #define ll long long #define INF 0x3f3f3f3f #define cle(a) memset(a,0,sizeof(a)) us

POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

Language: Default Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9631   Accepted: 4446 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John