hdu 1532 EK模板
我再网上摘的
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; #define N 300 int m; int Visited[N]; int Prev[N]; int G[N][N]; unsigned EKmin() { deque<int> q; memset(Visited,0,sizeof(Visited)); memset(Prev,0,sizeof(Prev)); Prev[1]=0; Visited[1]=1; q.push_back(1); bool bFindPath=false; while(!q.empty()) { int u=q.front(); q.pop_front(); for(int i=1;i<=m;i++) { if(G[u][i]>0&&!Visited[i]) { Prev[i]=u; Visited[i]=1; if(i==m) { bFindPath=true; q.clear(); break; } else q.push_back(i); } } } if(!bFindPath) return 0; int i=m; unsigned nMinFlow=999999999; while(Prev[i]) { if(nMinFlow>G[Prev[i]][i]) nMinFlow=G[Prev[i]][i]; i=Prev[i]; } i=m; while(Prev[i]) { G[Prev[i]][i]-=nMinFlow; G[i][Prev[i]]+=nMinFlow; i=Prev[i]; } return nMinFlow; } int main() { int n,i,u,v,val; while(scanf("%d%d",&n,&m)!=EOF) { memset(G,0,sizeof(G)); for(i=0;i<n;i++) { scanf("%d %d %d",&u,&v,&val); G[u][v]+=val; } unsigned nMinFlow=0; unsigned aug; while(aug=EKmin()) nMinFlow+=aug; printf("%d\n",nMinFlow); } return 0; }