题目:草地排水
思路:EK增广路算法求最大流模板
代码:
#include<bits/stdc++.h>
using namespace std;#define maxn 200
#define read(x) scanf("%d",&x);
#define inf (1<<30)int n,m;
int c[maxn+5][maxn+5];
int f[maxn+5][maxn+5];void readin() {read(m);read(n);for(int i=1;i<=m;i++) {int x,y,z;read(x);read(y);read(z);c[x][y]+=z;}
}queue<int> que,emp;
bool use[maxn+5];
int fa[maxn+5];bool bfs() {que=emp;memset(use,0,sizeof(use));use[1]=true;que.push(1);while(!que.empty()) {int h=que.front();que.pop();for(int i=1;i<=n;i++) {if(use[i]||f[h][i]>=c[h][i]) continue;que.push(i);fa[i]=h;use[i]=true;if(i==n) return true;}}return false;
}vector<int> vec;void count() {vec.clear();int k=n;while(k) {vec.push_back(k);k=fa[k];}
}void change() {count();int s=inf;for(int i=vec.size()-1;i>=1;i--) {int x=vec[i],y=vec[i-1];s=min(s,c[x][y]-f[x][y]);}for(int i=vec.size()-1;i>=1;i--) {int x=vec[i],y=vec[i-1];f[x][y]+=s;c[y][x]+=s;}
}int main() {readin();while(bfs()) {change();}int ans=0;for(int i=1;i<=n;i++) {ans+=f[1][i];}printf("%d",ans);return 0;
}