Skip to content

Commit e5094a2

Browse files
committed
boj 6086 최대 유량
1 parent 918b496 commit e5094a2

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

network flow/6086.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <algorithm>
5+
#define MAX 52
6+
#define INF 1000000000
7+
using namespace std;
8+
9+
vector<int> a[MAX];
10+
bool visit[MAX][MAX];
11+
int c[MAX][MAX], f[MAX][MAX], d[MAX];
12+
int N, result, w;
13+
char u, v;
14+
15+
char alpha(char x) {
16+
if (x < 97) return x - 65;
17+
else return x - 71;
18+
}
19+
20+
void maxflow(int s, int e) {
21+
while (1) {
22+
fill(d, d + MAX, -1);
23+
queue<int> q;
24+
q.push(s);
25+
while (!q.empty()) {
26+
int x = q.front();
27+
q.pop();
28+
29+
for (int i = 0; i < a[x].size(); i++) {
30+
int y = a[x][i];
31+
32+
if (c[x][y] - f[x][y] > 0 && d[y] == -1) {
33+
q.push(y);
34+
d[y] = x;
35+
if (y == e) break;
36+
}
37+
}
38+
}
39+
if (d[e] == -1) break;
40+
int flow = INF;
41+
42+
for (int i = e; i != s; i = d[i]) {
43+
flow = min(flow, c[d[i]][i] - f[d[i]][i]);
44+
}
45+
46+
for (int i = e; i != s; i = d[i]) {
47+
f[d[i]][i] += flow;
48+
f[i][d[i]] -= flow;
49+
}
50+
51+
result += flow;
52+
}
53+
}
54+
55+
int main() {
56+
cin.tie(NULL); cout.tie(NULL);
57+
ios::sync_with_stdio(false);
58+
59+
cin >> N;
60+
for (int i = 0; i < N; i++) {
61+
cin >> u >> v >> w;
62+
u = alpha(u);
63+
v = alpha(v);
64+
if (!visit[u][v]) {
65+
a[u].push_back(v);
66+
a[v].push_back(u);
67+
visit[u][v] = true;
68+
visit[v][u] = true;
69+
}
70+
c[u][v] += w;
71+
c[v][u] += w;
72+
}
73+
74+
maxflow(0, 25);
75+
76+
cout << result << '\n';
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)