最大流问题,一个裸体,也算是第一次接触最大流了。
主要是看的这个文章:
点击打开链接
描述的很清晰。
附上自己仿的代码:
#include<iostream> #include<cstdio> #include<string.h> #include<algorithm> using namespace std ; #define MAX 205 #define maxn 0x3f3f3f3f; #include<queue> int c[MAX][MAX]; int f[MAX][MAX]; int a[MAX]; int pre[MAX]; int bfs(int start ,int end ) { int flow = 0 ; queue<int> que ; while(!que.empty()) que.pop(); while(true) { memset(a,0,sizeof(a)); a[start] = maxn; que.push(start); while(!que.empty()) { int from = que.front(); que.pop(); for(int i = 1 ; i <= end ; i ++ ) { if(!a[i]&&c[from][i]>f[from][i]) { pre[i] = from ; que.push(i); a[i] = min(a[from],c[from][i]-f[from][i]); } } } if(!a[end]) break ; for(int j = end ; j!=start ; j = pre[j]) { f[pre[j]][j] += a[end];//this thought is f not c ; f[j][pre[j]] -= a[end]; } flow += a[end]; } return flow ; } int main(){ int M , N , i , j ,s,e,d; while(~scanf("%d%d",&N,&M)) { memset(c,0,sizeof(c)); memset(f,0,sizeof(f)); memset(pre,-1,sizeof(pre)); for(i = 0 ; i <N ; i ++) { scanf("%d%d%d",&s,&e,&d); c[s][e] +=d ; } printf("%d\n",bfs(1,M)); } return 0 ; }