网络流初步——增广路算法(EK)模板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <queue> 3 #include<string.h> 4 using namespace std; 5 #define arraysize 201 6 int maxData = 0x7fffffff; 7 int a[222][222]; 8 int flow[222]; 9 int pre[222]; 10 int n,m; 11 queue<int> q; 12 int BFS(int s,int t) 13 { 14 int i,j; 15 while(!q.empty()) 16 q.pop(); 17 for(i=1;i<m+1;i++) 18 { 19 pre[i]=-1; 20 } 21 pre[s]=0; 22 flow[s]= maxData; 23 q.push(s); 24 while(!q.empty()) 25 { 26 int index=q.front(); 27 q.pop(); 28 if(index==t) 29 break; 30 for(i=1;i<m+1;++i) 31 { 32 if(i!=s && a[index][i]>0 && pre[i]==-1) 33 { 34 pre[i]=index; 35 flow[i]=min(a[index][i],flow[index]); 36 q.push(i); 37 } 38 } 39 } 40 if(pre[t]==-1) 41 return -1; 42 else 43 return flow[t]; 44 } 45 int ek(int s,int t) 46 { 47 int increasement=0; 48 int sumflow=0; 49 while((increasement=BFS(s,t))!=-1) 50 { 51 int k=t; 52 while(k!=s) 53 { 54 int last=pre[k]; 55 a[last][k]-=increasement; 56 a[k][last]+=increasement; 57 k = last; 58 } 59 sumflow+=increasement; 60 } 61 return sumflow; 62 } 63 int main() 64 { 65 int i,j; 66 int start,end,ci; 67 cin>>n>>m; 68 memset(a,0,sizeof(a)); 69 memset(flow,0,sizeof(flow)); 70 for(i=0;i<n;i++) 71 { 72 cin>>start>>end>>ci; 73 a[start][end]+=ci; 74 } 75 cout<<ek(1,m)<<endl; 76 return 0; 77 }
O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~