Skip to content

Instantly share code, notes, and snippets.

@aajjbb
Created August 10, 2012 16:54
Show Gist options
  • Save aajjbb/3315592 to your computer and use it in GitHub Desktop.
Save aajjbb/3315592 to your computer and use it in GitHub Desktop.
Edmonds-Karp implementation
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#define MAXN 110
#define INF 1000000000;
using namespace std;
vector<int> graph[MAXN];
int capacity[MAXN][MAXN];
int max_flow(int source, int sink) {
int residual[MAXN][MAXN]; memset(residual, 0, sizeof(residual));
while(1) {
int prev[MAXN]; memset(prev, -1, sizeof(prev));
int actual[MAXN]; memset(actual, 0, sizeof(actual));
prev[source] = source;
actual[source] = INF;
queue<int> q; q.push(source);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(capacity[u][v] - residual[u][v] > 0 && prev[v] == -1) {
prev[v] = u;
actual[v] = min(actual[u], capacity[u][v] - residual[u][v]);
if(v != sink) {
q.push(v);
} else {
while(prev[v] != v) {
u = prev[v];
residual[u][v] += actual[sink];
residual[v][u] -= actual[sink];
v = u;
}
goto end;
}
}
}
}
end:;
if(prev[sink] == -1) {
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += residual[source][i];
}
return sum;
}
}
}
@halecakir
Copy link

You need to check again. There is a problem when compiling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment