-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathditch.cpp
executable file
·77 lines (74 loc) · 1.65 KB
/
ditch.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
/*
ID: paulius10
PROG: ditch
LANG: C++
*/
#include <algorithm>
#include <fstream>
#include <limits>
#include <vector>
using namespace std;
int main() {
ifstream fin("ditch.in");
int N, M;
fin >> N >> M;
int capacity[M][M];
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) capacity[i][j] = 0;
}
for (int i = 0; i < N; i++) {
int S, E, C;
fin >> S >> E >> C;
S--;
E--;
capacity[S][E] += C;
}
fin.close();
int sink = M - 1;
int totalflow = 0;
while (true) {
int prevnode[M], flow[M];
bool visited[M];
for (int i = 0; i < M; i++) {
prevnode[i] = -1;
flow[i] = 0;
visited[i] = false;
}
flow[0] = numeric_limits<int>::max();
int maxloc;
while (true) {
int maxflow = 0;
maxloc = -1;
for (int i = 0; i < M; i++) {
if (flow[i] > maxflow && !visited[i]) {
maxflow = flow[i];
maxloc = i;
}
}
if (maxloc == -1 || maxloc == sink) break;
visited[maxloc] = true;
for (int i = 0; i < M; i++) {
if (capacity[maxloc][i] <= 0) continue;
int m = min(maxflow, capacity[maxloc][i]);
if (flow[i] < m) {
prevnode[i] = maxloc;
flow[i] = m;
}
}
}
if (maxloc == -1) break;
int pathcapacity = flow[sink];
totalflow += pathcapacity;
int curnode = sink;
while (curnode != 0) {
int nextnode = prevnode[curnode];
capacity[nextnode][curnode] -= pathcapacity;
capacity[curnode][nextnode] += pathcapacity;
curnode = nextnode;
}
}
ofstream fout("ditch.out");
fout << totalflow << endl;
fout.close();
return 0;
}