网络流 KM dinic

时间:2022-11-09 13:29:33

study from:

https://blog.csdn.net/A_Comme_Amour/article/details/79356220

1.

Edmonds-Karp 无优化

最坏时间复杂度O(n*m*m) n为点数,m为边数

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
const int maxn=1e4+;
const int inf=1e9; struct node
{
int d,len;
node *next,*opp;
}*e[maxn],*pre[maxn]; int sum=,s,t,add[maxn];
queue<int> st;
bool vis[maxn]; void add_edge(int x,int y,int len)
{
node *p1=(node*) malloc (sizeof(node));
node *p2=(node*) malloc (sizeof(node)); p1->d=y;
p1->len=len;
p1->next=e[x];
p1->opp=p2;
e[x]=p1; p2->d=x;///注意
p2->len=;///注意
p2->next=e[y];
p2->opp=p1;
e[y]=p2;
} void bfs()
{
int d,dd,v;
node *p;
while ()
{
memset(add,,sizeof(add));
///vis不用初始化
add[s]=inf;
vis[s]=;
st.push(s);
while (!st.empty())
{
d=st.front();
st.pop();
p=e[d];
while (p)
{
dd=p->d;
v=min(add[d],p->len);
if (add[dd]<v)
{
add[dd]=v;
pre[dd]=p->opp;
if (!vis[dd])
{
vis[dd]=;
st.push(dd);
}
}
p=p->next;
}
vis[d]=;
} if (add[t]==)
break; sum+=add[t];
d=t;
while (d!=s)
{
pre[d]->len+=add[t];
pre[d]->opp->len-=add[t];
d=pre[d]->d;
}
}
} int main()
{
int n,m,x,y,z;
scanf("%d%d%d%d",&n,&m,&s,&t);
while (m--)
{
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
}
bfs();
printf("%d",sum);
return ;
}

2.

study from https://www.luogu.org/problemnew/solution/P3376 第一个题解的dinic

dinic

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
const int maxn=1e4+;
const int inf=1e9; struct node
{
int d,len;
node *next,*opp;
}*e[maxn]; int sum=,s,t;
int q[maxn],dep[maxn];
bool vis[maxn]; void add_edge(int x,int y,int len)
{
node *p1=(node*) malloc (sizeof(node));
node *p2=(node*) malloc (sizeof(node)); p1->d=y;
p1->len=len;
p1->next=e[x];
p1->opp=p2;
e[x]=p1; p2->d=x;
p2->len=;///注意
p2->next=e[y];
p2->opp=p1;
e[y]=p2;
} ///前面的网络流算法,每进行一次增广,都要做 一遍BFS,十分浪费。能否少做几次BFS? bool bfs()
{
int head=,tail=,d,dd;
node *p;
memset(vis,,sizeof(vis));
vis[s]=;
dep[s]=;
q[]=s; while (head<tail)
{
head++;
d=q[head];
p=e[d];
while (p)
{
dd=p->d;
if (p->len> && !vis[dd])
{
tail++;
q[tail]=dd;
vis[dd]=;
dep[dd]=dep[d]+;
}
p=p->next;
}
}
if (vis[t])
return ;
return ;
} ///DFS找到一条增广路径后,并不立即结束,而是回溯后继续DFS寻找下一个增广路径 int dfs(int d,int add)
{
if (!add || d==t)
return add;
int totf=,f,dd;
node *p=e[d];
while (p)
{
dd=p->d;
if (dep[dd]==dep[d]+ && (f=dfs(dd,min(add,p->len)))>)///注意
{
totf+=f;
add-=f;///注意
p->len-=f;
p->opp->len+=f;
}
p=p->next;
}
return totf;
} int main()
{
int n,m,x,y,z;
scanf("%d%d%d%d",&n,&m,&s,&t);
while (m--)
{
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
}
while (bfs())
sum+=dfs(s,inf);
printf("%d",sum);
return ;
}