////网络流算法的模板!!!
E K算法
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define N 210 #define INF 0x3f3f3f3f int maps[N][N], pre[N], n, m, vis[N], ans; int solve() { int f=0; queue<int> Q; memset(pre, 0, sizeof(pre)); Q.push(1); while(Q.size()) { int q=Q.front(); Q.pop(); if(q==m) { f=1; break; } for(int i=1; i<=m; i++) { if(maps[q][i]>0&&!pre[i]) { pre[i]=q; Q.push(i); } } } if(f==0) return 0; int k=m; int bian=INF; while(pre[k]&&k!=1) { bian=min(bian, maps[pre[k]][k]); k=pre[k]; } k=m; while(pre[k]&&k!=1) { maps[pre[k]][k]-=bian; maps[k][pre[k]]+=bian; k=pre[k]; } return bian; } int main() { int a, b, c, x; while(~scanf("%d%d", &n, &m)) { memset(maps, 0, sizeof(maps)); for(int i=1; i<=n; i++) { scanf("%d%d%d", &a, &b, &c); maps[a][b]+=c; } ans=0; while(x=solve()) ans+=x; printf("%d ", ans); } return 0 ; }