标准大白书式模板,代码简单但由于效率并不高,所以并不常用,就是这样
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 const int maxm=150+5; 8 const int INF=0x3f3f3f3f; 9 10 struct edge{ 11 int from,to,c,f; 12 edge(int a,int b,int m,int n):from(a),to(b),c(m),f(n){} 13 }; 14 15 struct ek{ 16 int n,m; 17 vector<edge>e; 18 vector<int>g[maxm]; 19 int a[maxm]; 20 int p[maxm]; 21 22 void init(int n){ 23 for(int i=0;i<n+3;i++)g[i].clear(); 24 e.clear(); 25 } 26 27 void add(int from,int to,int c){ 28 e.push_back(edge(from,to,c,0)); 29 e.push_back(edge(to,from,0,0)); 30 m=e.size(); 31 g[from].push_back(m-2); 32 g[to].push_back(m-1); 33 } 34 35 int mf(int s,int t){ 36 int f=0; 37 while(1){ 38 memset(a,0,sizeof(a)); 39 queue<int>q; 40 q.push(s); 41 a[s]=INF; 42 while(!q.empty()){ 43 int u=q.front(); 44 q.pop(); 45 for(int i=0;i<g[u].size();i++){ 46 edge tmp=e[g[u][i]]; 47 if(!a[tmp.to]&&tmp.c>tmp.f){ 48 p[tmp.to]=g[u][i]; 49 a[tmp.to]=min(a[u],tmp.c-tmp.f); 50 q.push(tmp.to); 51 } 52 } 53 if(a[t])break; 54 } 55 if(!a[t])break; 56 for(int i=t;i!=s;i=e[p[i]].from){ 57 e[p[i]].f+=a[t]; 58 e[p[i]^1].f-=a[t]; 59 60 } 61 f+=a[t]; 62 } 63 return f; 64 } 65 };
时间: 2024-10-26 09:35:58