-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_flow2.cpp
More file actions
78 lines (70 loc) · 1.41 KB
/
max_flow2.cpp
File metadata and controls
78 lines (70 loc) · 1.41 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
/*
Edmonds-Karp
*/
#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 105;
const int inf = 1e9;
int cap[MAX_V][MAX_V];
int parnt[MAX_V];
vector<int> g[MAX_V];
int maxflow(int s, int t){
int mf = 0;
while(1){
memset(parnt, -1, sizeof(parnt));
parnt[s] = -2;
queue<int> q;
q.push(s);
while(!q.empty()){
int v = q.front(); q.pop();
if(v == t){
break;
}
int sz = g[v].size();
for(int i = 0; i < sz; i++){
int w = g[v][i];
if(parnt[w] == -1 && cap[v][w] > 0){
parnt[w] = v;
q.push(w);
}
}
}
if(parnt[t] == -1) break;
int bot = inf;
for(int v = t, w = parnt[t]; w >= 0; v = w, w = parnt[w]){
bot = min(bot, cap[w][v]);
}
for(int v = t, w = parnt[t]; w >= 0; v = w, w = parnt[w]){
cap[w][v] -= bot;
cap[v][w] += bot;
}
mf += bot;
}
return mf;
}
int main()
{
int nn, mm, kk;
int inst = 1;
while(scanf("%d %d %d", &nn, &mm, &kk) != EOF){
memset(cap, 0, sizeof(cap));
/* se o grafo for bipartido:
int s = 0;
int t = nn + mm + 1;
int nv = t + 1;
*/
for(int i = 0; i <= nv; i++){
g[i].clear();
}
for(int uu, vv, qq = 1; qq <= kk; qq++){
scanf("%d %d", &uu, &vv); // uu -> vert. do conjunto A, vv -> vert. do conjunto B
/*
g[uu].push_back(vv);
g[vv].push_back(uu);
cap[uu][vv] += 1;
*/
}
int ans = maxflow(s, t);
}
return 0;
}