EK模板
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<vector> 6 using namespace std; 7 const int maxn=205; 8 const int maxm=205; 9 const int inf=1e9; 10 struct edge{ 11 int from,to,flow; 12 edge(int u,int v,int f):from(u),to(v),flow(f) {} 13 }; 14 int n,s,t; 15 vector<edge>E; //大小为边数的两倍 ,用于存边 16 vector<int>G[maxn]; //存每个点对应的边 17 int a[maxn],p[maxn]; //a代表每个点的流出量,p用于记录进入该点的那条边的编号 18 19 void init() //初始化 20 { 21 for ( int i=0;i<n;i++ ) G[i].clear(); 22 E.clear(); 23 } 24 25 void addedge(int from,int to,int flow) 26 { 27 E.push_back(edge(from,to,flow)); 28 E.push_back(edge(to,from,0)); 29 int m=E.size(); 30 G[from].push_back(m-2); 31 G[to].push_back(m-1); 32 } 33 34 int maxflow() 35 { 36 int flow=0; 37 for ( ;; ) { 38 memset(a,0,sizeof(a)); 39 a[s]=inf; 40 queue<int>que; 41 que.push(s); 42 while ( !que.empty() ) { 43 int u=que.front(); 44 que.pop(); 45 for ( int i=0;i<G[u].size();i++ ) { 46 int v=G[u][i]; 47 edge& e=E[v]; 48 if ( e.flow>0 && !a[e.to] ) { 49 p[e.to]=v; 50 a[e.to]=min(a[u],e.flow); 51 que.push(e.to); 52 } 53 } 54 if ( a[t] ) break; 55 } 56 if ( !a[t] ) break; 57 for ( int u=t;u!=s;u=E[p[u]].from ) { 58 E[p[u]].flow-=a[t]; 59 E[p[u]^1].flow+=a[t]; 60 } 61 flow+=a[t]; 62 } 63 return flow; 64 }