forked from lprimeroo/DSA
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FFA.cpp
103 lines (91 loc) · 2.22 KB
/
FFA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//saru95
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <complex>
#include <math.h>
#include <utility>
#include <cmath>
#include <limits.h>
#include <set>
#include <vector>
#include <map>
#include <cctype>
#include <queue>
#include <stdio.h>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <numeric>
#include <memory.h>
#define all(a) (a).begin(),(a).end()
#define gcd __gcd
#define bitcount __builtin_popcount
using namespace std;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef std::pair<int, int> pii;
typedef std::set<int> si;
typedef std::map<std::string, int> msi;
int nodes , parent;
bool bfs(int rgraph[nodes][nodes], int source, int sink) {
bool visited[nodes] ;
memset(visited,false, sizeof(visited)) ;
queue<int> q ;
q.push(source) ;
visited[source] = true ;
parent[source] = -1 ;
while(!q.empty()) {
int u = q.front() ;
q.pop() ;
for(int i=0;i<nodes;i++) {
if(visited[i]==false && rgraph[u][i] > 0){
q.push(i) ;
parent[i] = u ;
visited[i] = true ;
}
}
}
return (visited[sink]==true) ;
}
void FFA(int graph[nodes][nodes], int source, int sink) {
int rgraph[nodes][nodes], u ;
for(int i=0;i<nodes,i++) {
for(int j=0;j<nodes;j++) {
rgraph[i][j] = graph[i][j] ; //initialising Residual Graph
}
}
int maxflow = 0 ; //parent used by bfs to store parent
parent = new int[nodes] ;
while (bfs(rgraph, source, sink)) {
int parentflow = INT_MAX ;
for(int i=sink;i!=source;i=parent[i]) { //finding min capacity for a path
u = parent[i] ;
parentflow = min(parentflow, rgraph[u][i]) ;
}
for(int i=sink;i!=source;i=parent[i]) {
u = parent[i] ;
rgraph[u][i] -= parentflow ;
rgraph[i][u] += parentflow ; //adding to the reverse edge
}
maxflow += parentflow ;
}
return maxflow ;
}
int main(int argc, char const *argv[]) {
cin >> nodes ;
int graph[nodes][nodes] ;
for(int i=0;i<nodes;i++) {
for(int j=0;j<nodes;j++) {
cin >> graph[i][j] ;
}
}
cin >> start >> sink ;
FFA(graph, source, sink) ;
return 0;
}