-
Notifications
You must be signed in to change notification settings - Fork 0
/
EK.cpp
80 lines (74 loc) · 1.47 KB
/
EK.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N = 4e2 + 10;
const int INF = 0x3f3f3f3f;
int maxData = 0x7f7f7f7f;
int capacity[N][N];
int flow[N];
int pre[N];
int n,m;
queue<int> myqueue;
int bfs(int src, int des){
int i,j;
while(!myqueue.empty())
myqueue.pop();
for(i=1;i<=n;i++){
pre[i] = -1;
}
pre[src] = 0;
flow[src] = maxData;
myqueue.push(src);
while(!myqueue.empty()){
int index = myqueue.front();
myqueue.pop();
if(index == des)
break;
for(i=1;i<=n;i++){
if(i!=src && capacity[index][i] > 0 && pre[i] == -1){
pre[i] = index;
flow[i] = min( capacity[index][i], flow[index] );
myqueue.push(i);
}
}
}
if(pre[des] == -1)
return -1;
else
return flow[des];
}
int maxFlow(int src, int des){
int increasement = 0;
int sumflow = 0;
while( (increasement = bfs(src, des)) != -1){
int k = des;
while(k != src){
int last = pre[k];
capacity[last][k] -= increasement;
capacity[k][last] += increasement;
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main(int argc, char **argv)
{
int i,j;
int start, ed, ci, st, se;
//freopen("testdata.in" , "r", stdin);
//freopen("aaa.out","w", stdout );
// O(n * m ^ 2);
while(cin >> m >> n){
memset(capacity, 0, sizeof(capacity));
memset(flow, 0, sizeof(flow));
for(i=0;i<m;i++){
cin >> start >> ed >> ci;
if(start == ed)
continue;
capacity[start][ed] += ci;
}
cout << maxFlow(1,n) << endl;
}
return 0;
}