网络流算法:
关于网络流是什么
#include <queue>
#include <cstdio>
#include <string>
#include <iostream>
#include <climits>
#include <unordered_map>
using namespace std;
const int MAXN = 550;
int n; // 图中点的数目
int pre[MAXN]; // 从 s - t 中的一个可行流中, 节点 i 的前序节点为 Pre[i];
bool vis[MAXN]; // 标记一个点是否被访问过
int mp[MAXN][MAXN]; // 记录图信息
unordered_map<string,int> pos;
bool bfs(int s, int t){
queue <int> que;
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
pre[s] = s;
vis[s] = true;
que.push(s);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = 1; i <= n; i++){
if(mp[u][i] && !vis[i]){
pre[i] = u;
vis[i] = true;
if(i == t) return true;
que.push(i);
}
}
}
return false;
}
int EK(int s, int t){
int ans = 0;
while(bfs(s, t)){
int mi = INT_MAX;
for(int i = t; i != s; i = pre[i]){
mi = min(mi, mp[pre[i]][i]);
}
for(int i = t; i != s; i = pre[i]){
mp[pre[i]][i] -= mi;
mp[i][pre[i]] += mi;
}
ans += mi;
}
return ans;
}
int main(){
string s,d;
cin>>s>>d>>n;
pos[s] = 1;
pos[d] = 2;
int cnt = 3;
for(int i = 0;i < n;i++){
string u,v;
int capa;
cin>>u>>v>>capa;
if(pos[u] == 0) pos[u] = cnt++;
if(pos[v] == 0) pos[v] = cnt++;
mp[pos[u]][pos[v]] = capa;
}
printf("%d",EK(1,2));
system("pause");
return 0;
}