-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_network.cpp
More file actions
111 lines (95 loc) · 2.08 KB
/
Copy pathflow_network.cpp
File metadata and controls
111 lines (95 loc) · 2.08 KB
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
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
using namespace std;
bool bfs(int **graph, int s, int t, int parent[], int V)
{
vector<bool> visited(V, false);
queue<int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = 0; v < V; v++)
{
if (visited[v] == false && graph[u][v] > 0)
{
if (v == t)
{
parent[v] = u;
return true;
}
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
return false;
}
int fordFulkerson(int **graph, int s, int t, int V)
{
int u, v;
int parent[V];
int max_flow = 0;
while (bfs(graph, s, t, parent, V))
{
int path_flow = INT_MAX;
for (v = t; v != s; v = parent[v])
{
u = parent[v];
path_flow = min(path_flow, graph[u][v]);
}
for (v = t; v != s; v = parent[v])
{
u = parent[v];
graph[u][v] -= path_flow;
graph[v][u] += path_flow;
}
max_flow += path_flow;
}
return max_flow;
}
int main()
{
int V, E;
cout << "Enter the number of vertices and Edges: ";
cin >> V >> E;
int **graph;
graph = new int *[V];
for (int i = 0; i < V; i++)
{
graph[i] = new int[V];
for (int j = 0; j < V; j++)
{
graph[i][j] = 0;
}
}
cout << "Enter u v w: ";
for (int i = 0; i < E; i++)
{
int u, v, w;
cin >> u >> v >> w;
graph[u][v] = w;
}
cout << "Enter source and sink: ";
int s, t;
cin >> s >> t;
cout << "The maximum possible flow is "
<< fordFulkerson(graph, s, t, V) << endl;
return 0;
}
// input:
// 6 10
// 0 1 16
// 0 2 13
// 1 2 10
// 2 1 4
// 1 3 12
// 2 4 14
// 3 2 9
// 4 3 7
// 3 5 20
// 4 5 4
// 0 5