ISAP是对SAP进行优化后的算法,ISAP时间复杂度为O(V^2E),SAP的时间复杂度为O(VE^2)
SAP
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- #include <queue>
- using namespace std;
- const int maxn = 100;
- const int INF = (1 << 30) - 1;
- int g[maxn][maxn];//残余网络
- int f[maxn][maxn];//实流网络
- int Prev[maxn];//保存前驱节点
- bool visited[maxn];
- int n, m;
- bool BFS(int s,int t)
- {
- int i;
- memset(Prev, -1, sizeof(Prev));
- memset(visited, false, sizeof(visited));//初始化
- queue<int> q;
- visited[s] = true;
- q.push(s);
- while (!q.empty())
- {
- int now = q.front();
- q.pop();
- for (i = 1; i <= n; i++)
- {
- if (!visited[i] && g[now][i]>0)//没被访问并且有边相连
- {
- visited[i] = true;
- Prev[i] = now;
- if (i == t)return true;//找到一条增广路劲
- q.push(i);
- }
- }
- }
- return false;
- }
- int EK(int s, int t)
- {
- int v, w, d, maxflow;
- maxflow = 0;
- while (BFS(s, t))//还有增广路径
- {
- v = t;
- d = INF;
- while (v != s)//向前找
- {
- w = Prev[v];
- if (d > g[w][v])
- {
- d = g[w][v];
- }
- v = w;
- }
- maxflow +=