-
Notifications
You must be signed in to change notification settings - Fork 1
/
#1089 Drainage Ditches
75 lines (67 loc) · 1.76 KB
/
#1089 Drainage Ditches
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
#include <iostream>
#include <cstring>
#include<queue>
#define INFINITY 2147483647
#define MAX_N 205
using namespace std;
int network[MAX_N][MAX_N];
bool visited[MAX_N];
int root[MAX_N];
int num_of_edge, num_of_ver;
int max_flow() {
int mflow = 0;
while (1) {
memset(visited, 0, sizeof(visited));
memset(root, 0, sizeof(root));
queue<int> Q;
visited[1] = true;
Q.push(1);
while (!Q.empty()) {
int cnt = Q.front();
Q.pop();
if (cnt == num_of_ver) {
break;
}
for (int i = 1; i <= num_of_ver; i++) {
if (!visited[i] && network[cnt][i] > 0) {
visited[i] = true;
Q.push(i);
root[i] = cnt;
}
}
}
if (!visited[num_of_ver]) {
break;
}
int minx = INFINITY;
for (int i = num_of_ver; i != 1; i = root[i]) {
minx = min(network[root[i]][i], minx);
}
for (int i = num_of_ver; i != 1; i = root[i]) {
network[root[i]][i] -= minx;
network[i][root[i]] += minx;
}
mflow += minx;
}
return mflow;
}
int main() {
cin >> num_of_edge >> num_of_ver;
memset(network, 0, sizeof(network));
for (int i = 0; i < num_of_edge; i++) {
int u, v, cap;
cin >> u >> v >> cap;
network[u][v] += cap;
}
cout << max_flow() << endl;
//system("pause");
return 0;
}
/**************************************************************
Problem: 1089
User: 22920142203903
Language: C++
Result: Accepted
Time:0 ms
Memory:1672 kb
****************************************************************/