-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path14.max flow.cpp
More file actions
84 lines (72 loc) · 1.55 KB
/
Copy path14.max flow.cpp
File metadata and controls
84 lines (72 loc) · 1.55 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
#include<bits/stdc++.h>
using namespace std;
vector <int> adj1[100];
int cf[100][100],parent[100];
int call(int d);
bool maxflow(int n, int s, int d)
{
queue <int> q;
int color[100], check = false;
for(int i = 1; i <= n; i++)
{
color[i] = false;
parent[i] = -1;
}
q.push(s);
color[s] = true;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 0; i < adj1[u].size(); i++)
{
int v = adj1[u][i];
if(color[v] == false && cf[u][v]>0)
{
color[v] = true;
q.push(v);
parent[v] = u;
}
}
}
return color[d];
}
int call(int n, int s, int d)
{
int mf = 0;
while(maxflow(n, s, d))
{
int m = INT_MAX;
for (int i = d; i != s; i = parent[i])
{
int u = parent[i];
int v = i;
m = min(m,cf[u][v]);
}
for(int i = d; i != s; i = parent[i])
{
int u = parent[i];
int v = i;
cf[u][v] = cf[u][v] - m;
cf[v][u] = cf[v][u] + m;
}
mf = mf + m;
}
return mf;
}
int main()
{
int n, e;
cout << "Enter nodes and edges\n";
cin >> n >> e;
cout << "Enter edges u to v and capacity of u to v\n";
for (int i = 0; i < e; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj1[u].push_back(v);
adj1[v].push_back(u);
cf[u][v] = w;
}
cout <<"maximum flow is: " << call(n, 1, 6);
}