思路
裸的网络流!!
本题与普通的网络流有2点不同 (其实根本没有区别):
(1) 当 A —3—> B —5—> C 时,它会合并成一条 A —3—> C 的边时: 我们可以发现普通的网络流跑以上图时最大流即为 min(3, 5) = 3,本质上就与普通的网络流没有区别
So,我们可以忽略第一个不同
(2) 当 A —3—> B 且还有 A —5—> B 时,它会合并成一条 A —8—> B 的边时: 我们可以发现普通的网络流跑以上图时最大流即为 3+5 = 8,本质上就也与普通的网络流没有区别
So,我们可以忽略第二个不同
于是本题其实就 = 普通的网络流
本人使用了Dinic算法,如有需要学习可以去模板最大流
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int e[maxn], w[maxn], ne[maxn], h[maxn], idx;
int d[maxn], cur[maxn];
int n, m, S, T;
void add(int a, int b, int c) {
e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++;
}
bool bfs(int x) {
queue<int> q;
q.push(x);
memset(d, -1, sizeof(d));
d[x] = 0;
cur[x] = h[x];
while(!q.empty()) {
int k = q.front();
q.pop();
for(int i=h[k]; i!=-1; i=ne[i]) {
int nw = e[i];
if(d[nw] == -1 && w[i]) {
d[nw] = d[k] + 1;
cur[nw] = h[nw];
if(nw == T) return true;
q.push(nw);
}
}
}
return false;
}
int find(int x, int limit) {
if(x == T) return limit;
int cnt = 0;
for(int i=cur[x]; i!=-1 && cnt < limit; i=ne[i]) {
cur[x] = i;
int nw = e[i];
if(d[nw] == d[x] + 1 && w[i]) {
int tmp = find(nw, min(w[i], limit-cnt));
if(tmp == 0) {
d[nw] = -1;
}
w[i] -= tmp;
w[i^1] += tmp;
cnt += tmp;
}
}
return cnt;
}
int dinic() {
int ans = 0, tmp = 0;
while(bfs(S)) {
while(tmp = find(S, 0x3f3f3f3f)) {
ans += tmp;
}
}
return ans;
}
int main() {
memset(h, -1, sizeof(h));
cin >> m;
S = 0;
T = 25;
for(int i=1; i<=m; i++) {
char cx, cy;
int x, y, k;
cin >> cx >> cy >> k;
x = cx-'A';
y = cy-'A';
add(x, y, k);
add(y, x, 0);
}
cout << dinic();
return 0;
}