思路:从终点到起点BFS 一次 对所有节点分层,然后每次找一条增广路,满足可行弧,如果找不到对当前已找到的最后一条层数+1,回退一步,继续找,直到d[s]>n-1,因为n个点 最多分成0~n-1层,如果大于,说明中间有断层,不用再搜了!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define LL long long
#define N 50
#define M 1006
#define DEBUG puts("It's here!")
#define INF 1<<29
#define CLS(x,v) memset(x,v,sizeof(x))
#define FOR(i,a,n) for(int i=(a);i<=(n);++i)
int n,cap[M][M],r[M][M];//remainder
int maxflow,d[M];//存储层次
//int s,e,n; 起点终点 和节点数
void bfs(int e,int n)
{
CLS(d,-1);
queue<int>Q;
Q.push(e);//从终点开始
int pre;
d[e]=0;
while(!Q.empty()){
pre=Q.front();Q.pop();
for(int i=0;i<n;i++)
if(d[i]==-1&&cap[i][pre]>0){
d[i]=d[pre]+1;
Q.push(i);
}
}
}
int father[M],low[M];// father保存路径,low保存路径上的最小流量
int sap(int s,int e,int n)
{
int pre=s,i,j;
bfs(e,n);maxflow=0;
low[s]=INF;
while(d[s]<n)
{
for(i=0;i<n;i++)
if(r[pre][i]>0&&d[pre]==d[i]+1)
break;
if(i<=n)//找到了允许弧
{
low[i]=min(r[pre][i],low[pre]);
father[i]=pre;pre=i;
if(i==e)//找到了最短增广路
{
maxflow+=low[e];
while(i!=s)
{
r[father[i]][i]-=low[e];
r[i][father[i]]+=low[e];
i=father[i];
}
pre=s;//从头再搜
}
}else
{
int mind=INF;
for(j=0;j<n;j++)
if(r[pre][j]>0&&mind>d[j]+1)
{
mind=d[j]+1;
}
d[pre]=mind;
if(pre!=s)pre=father[pre];//舍弃当前可行弧,回退一步
}
}
return maxflow;
}
int main()
{
return 0;
}