[Link] https://www.acmicpc.net/problem/6086
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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define command_param int argc, char *argv[]
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
const int INF = 987654321;
int n;
int capacity[52][52];
int flow[52][52];
vector<vector<int>> adj;
int convert(char ch) {
return ch > 'Z' ? ch - 'a' + 26 : ch - 'A';
}
int solve() {
queue<int> q;
int flow_max = 0;
while(1) {
q = {}; q.push(0);
int prev[52];
fill(prev, prev + 52, -1);
while(!q.empty()) {
int cur = q.front(); q.pop();
if(cur == 25) break;
for(int next : adj[cur]) {
if(prev[next] == -1 && capacity[cur][next] - flow[cur][next]) {
q.push(next);
prev[next] = cur;
}
}
}
if(prev[25] == -1) break;
int min_flow = INF;
for(int s = 25; s != 0; s = prev[s]) {
min_flow = min(min_flow, capacity[prev[s]][s] - flow[prev[s]][s]);
}
for(int s = 25; s != 0; s= prev[s]) {
flow[prev[s]][s] += min_flow;
flow[s][prev[s]] -= min_flow;
}
flow_max += min_flow;
}
return flow_max;
}
int main() {
FASTIO;
cin >> n;
adj.resize(52);
for0(i, n) {
char from, to; int c;
cin >> from >> to >> c;
int cfrom = convert(from), cto = convert(to);
capacity[cfrom][cto] += c;
capacity[cto][cfrom] += c;
adj[cfrom].push_back(cto);
adj[cto].push_back(cfrom);
}
cout << solve();
return 0;
}