https://blog.csdn.net/qq_21120027/article/details/51291314
递归邻接表
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_N=100; //顶点数上限
const int MAX_M=10000; //总的边数上限
struct edge{
int v,c,next; //v指另一个顶点,c表示容量。
}e[MAX_M];
int p[MAX_N],eid;
void init(){
memset(p,-1,sizeof(p));
eid=0;
}
void insert(int u,int v,int c){ //插入一条从u向v,容量为c的弧。
e[eid].v=v;
e[eid].next=p[u];
e[eid].c=c;
p[u]=eid++;
}
void addedge(int u,int v,int c){ //用insert插入网络中的弧
insert(u,v,c);
insert(v,u,0); //插入一条反方向,当前容量为0的弧
}
int S,T; //S是源点,T是汇点。
int d[MAX_N]; //存储每个顶点的层次
bool bfs(){
memset(d,-1,sizeof(d));
queue<int> q;
q.push(S);
d[S]=0;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=p[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(e[i].c>0&&d[v]==-1){
q.push(v);
d[v]=d[u]+1;
}
}
}
return (d[T]!=-1);
}
int dfs(int u,int flow){ //flow表示当前搜索分支的流量上限
if(u==T){
return flow;
}
int res=0;
for(int i=p[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(e[i].c>0&&d[u]+1==d[v]){
int tmp=dfs(v,min(flow,e[i].c)); // 递归计算顶点 v,用 c(u, v) 来更新当前流量上限
flow-=tmp;
e[i].c-=tmp;
res+=tmp;
e[i^1].c+=tmp; // 修改反向弧的容量
if(flow==0){ // 流量达到上限,不必继续搜索了
break;
}
}
}
if(res==0){ // 当前没有经过顶点 u 的可行流,不再搜索顶点 u
d[u]=-1;
}
return res;
}
int dinic(){ // 函数返回值就是最大流的结果
int res=0;
while(bfs()){
res+=dfs(S,INF); // 初始流量上限为 INF
}
return res;
}
int main() {
int n,m;
init();
cin>>m>>n;
for(int i=0;i<m;i++){
int u,v,flow;
cin>>u>>v>>flow;
addedge(u,v,flow);
}
cin>>S>>T;
cout<<dinic()<<endl;
return 0;
}
递归邻接矩阵
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110
int e[N][N];
int S,T;
int n,np,nc,m;
int d[N];
bool bfs(){
memset(d,-1,sizeof(d));
queue<int> q;
q.push(S);
d[S]=0;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<n;i++){
if(e[u][i]&&d[i]==-1){
d[i]=d[u]+1;
q.push(i);
}
}
}
return d[T]!=-1;
}
int dfs(int u,int flow){
if(u==T){
return flow;
}
int res=0;
for(int i=0;i<n;i++){
if(e[u][i]&&d[i]==d[u]+1){
int tmp=dfs(i,min(flow,e[u][i]));
flow-=tmp;
e[u][i]-=tmp;
res+=tmp;
e[i][u]+=tmp;
if(flow==0) break;
}
}
if(res==0) d[u]=-1;
return res;
}
int dinic(){
int res=0;
while(bfs()){
res+=dfs(S,INF);
}
return res;
}
//#define debug
int main(){
#ifdef debug
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // debug
int x,y,z;
char s[10];
while(scanf("%d%d",&n,&m)!=EOF){
while(m--){
scanf("%d%d%d",&x,&y,&z);
if(x==y) continue;
e[x][y]+=z;
}
scanf("%d%d",&S,&T);
printf("%d\n",dinic());
}
return 0;
}
非递归
//from kuangbin
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2010;
const int MAXM=1200010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,next,cap,flow;
}edge[MAXM];
int tol,head[MAXN];
void init(){
tol=2;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0){
edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
edge[tol].next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;
edge[tol].next=head[v];head[v]=tol++;
}
int Q[MAXN];
int dep[MAXN],cur[MAXN],sta[MAXN];
bool bfs(int s,int t,int n){
int front=0,tail=0;
memset(dep,-1,sizeof(dep[0])*(n+1));
dep[s]=0;
Q[tail++]=s;
while(front<tail){
int u=Q[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dep[v]==-1){
dep[v]=dep[u]+1;
if(v==t){
return true;
}
Q[tail++]=v;
}
}
}
return false;
}
int dinic(int s,int t,int n){
int maxflow=0;
while(bfs(s,t,n)){
for(int i=0;i<n;i++){
cur[i]=head[i];
}
int u=s,tail=0;
while(cur[s]!=-1){
if(u==t){
int tp=INF;
for(int i=tail-1;i>=0;i--){
tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
}
maxflow+=tp;
for(int i=tail-1;i>=0;i--){
edge[sta[i]].flow+=tp;
edge[sta[i]^1].flow-=tp;
if(edge[sta[i]].cap-edge[sta[i]].flow==0){
tail=i;
}
}
u=edge[sta[tail]^1].to;
}else if(cur[u]!=-1&&edge[cur[u]].cap>edge[cur[u]].flow&&dep[u]+1==dep[edge[cur[u]].to]){
sta[tail++]=cur[u];
u=edge[cur[u]].to;
}else{
while(u!=s&&cur[u]==-1){
u=edge[sta[--tail]^1].to;
}
cur[u]=edge[cur[u]].next;
}
}
}
return maxflow;
}
int main(){
int n,m,s,t;
scanf("%d%d%d%d",&n,&m,&s,&t);
int u,v,w;
init();
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
printf("%d\n",dinic(s,t,n));
return 0;
}