标签:
1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 struct edge 9 { 10 int from,to,cap,flow; 11 edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}; 12 }; 13 14 const int inf=999999; 15 int m,n; 16 int a[205]; 17 int p[205]; 18 vector<int>G[405]; 19 vector<edge>edges; 20 21 void init() 22 { 23 for(int i=0;i<n;i++) 24 G[i].clear(); 25 edges.clear(); 26 } 27 28 void addedge(int from,int to,int cap) 29 { 30 edges.push_back(edge(from,to,cap,0)); 31 edges.push_back(edge(to,from,0,0)); 32 int m=edges.size(); 33 G[to].push_back(m-1); 34 G[from].push_back(m-2); 35 } 36 37 int maxflow(int s,int t) 38 { 39 int flow=0; 40 while(1) 41 { 42 memset(a,0,sizeof(a)); 43 queue<int>q; 44 q.push(s); 45 a[s]=inf; 46 while(!q.empty()) 47 { 48 int x=q.front(); 49 q.pop(); 50 for(int i=0;i<G[x].size();i++) 51 { 52 edge& e=edges[G[x][i]]; 53 if(!a[e.to]&&e.cap>e.flow) 54 { 55 p[e.to]=G[x][i]; 56 a[e.to]=min(a[x],e.cap-e.flow); 57 q.push(e.to); 58 } 59 } 60 if(a[t])break; 61 } 62 if(!a[t])break; 63 for(int u=t;u!=s;u=edges[p[u]].from) 64 { 65 edges[p[u]].flow+=a[t]; 66 edges[p[u]^1].flow-=a[t]; 67 } 68 flow+=a[t]; 69 } 70 return flow; 71 } 72 73 int main() 74 { 75 int a,b,c; 76 while(scanf("%d%d",&m,&n)!=EOF) 77 { 78 init(); 79 for(int i=0;i<m;i++) 80 { 81 scanf("%d%d%d",&a,&b,&c); 82 addedge(a,b,c); 83 } 84 int ans=maxflow(1,n); 85 printf("%d\n",ans); 86 } 87 return 0; 88 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4721031.html