generated from cepdnaclk/eYY-XXX-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tempCodeRunnerFile.c
90 lines (67 loc) · 1.74 KB
/
tempCodeRunnerFile.c
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
85
86
87
88
89
90
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define MAX 105
int capacity[MAX][MAX], residual[MAX][MAX];
int parent[MAX];
int n, m, s, p;
int line[MAX];
int front, rear;
void addToLine(int x) {
line[rear++] = x;
}
int removeFromLine() {
return line[front++];
}
int isEmpty() {
return front == rear;
}
int search(int source, int sink) {
memset(parent, -1, sizeof(parent));
front = rear = 0;
addToLine(source);
parent[source] = source;
while (!isEmpty()) {
int u = removeFromLine();
for (int v = 1; v <= n; v++) {
if (parent[v] == -1 && residual[u][v] > 0) {
parent[v] = u;
if (v == sink)
return 1;
addToLine(v);
}
}
}
return 0;
}
int maxFlow(int source, int sink) {
int maxFlow = 0;
memcpy(residual, capacity, sizeof(capacity));
while (search(source, sink)) {
int pathFlow = INT_MAX;
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
if (residual[u][v] < pathFlow)
pathFlow = residual[u][v];
}
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
residual[u][v] -= pathFlow;
residual[v][u] += pathFlow;
}
maxFlow += pathFlow;
}
return maxFlow;
}
int main() {
scanf("%d %d %d %d", &n, &m, &s, &p);
memset(capacity, 0, sizeof(capacity));
for (int i = 0; i < m; i++) {
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
capacity[u][v] += c;
}
int maxPeopleTransported = maxFlow(s, n);
printf("%d\n", maxPeopleTransported < p ? maxPeopleTransported : p);
return 0;
}