链接:click here
代码:
#include <ctype.h> //最大流 入门 #include <stdio.h> #include <vector> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1101; const int inf=0x3f3f3f3f; struct edge {int to,cap,rev;}; vector<edge > G[maxn]; //图的邻接表表示 bool used[maxn]; //访问标记 void add_edge(int from,int to,int cap) { G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } int dfs(int v,int t,int f) { if(v==t) return f; used[v]=true; for(int i=0;i<G[v].size();i++) { edge &e=G[v][i]; if(!used[e.to]&&e.cap>0) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; for(;;) { memset(used,0,sizeof(used)); int f=dfs(s,t,inf); if(f==0) return flow; flow+=f; } } int main() { int n,m,too,capp,revv; while(~scanf("%d%d",&n,&m)) { memset(G,0,sizeof(G)); for(int i=0;i<n;i++) { scanf("%d%d%d",&too,&capp,&revv); add_edge(too,capp,revv); } printf("%d\n",max_flow(1,m)); } return 0; }
#include <cstdio> #include <vector> #include <iostream> #include <queue> #include <cstring> using namespace std; const int N = 300; const int MAX = 0x3f3f3f3f; int map[N][N]; int flow[N][N]; int a[N],p[N]; int Ford_fulkerson(int s,int t) { queue<int> qq; memset(flow,0,sizeof(flow)); int f=0,u,v; while(1) { memset(a,0,sizeof(a)); a[s]=MAX; qq.push(s); while(!qq.empty()) { u=qq.front();qq.pop(); for(v=1;v<=t;v++) { if(!a[v]&&map[u][v]>flow[u][v])//找到新结点v { p[v]=u;qq.push(v);//记录v的父亲,并加入FIFO队列 a[v]=a[u]<map[u][v]-flow[u][v]?a[u]:map[u][v]-flow[u][v];//a[v]为s-v路径上的最小流量 } } } if(a[t]==0) return f; for(int i=t;i!=s;i=p[i]) { flow[i][p[i]]-=a[t]; flow[p[i]][i]+=a[t]; } f+=a[t]; } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(map,0,sizeof(map)); for(int i=0;i<n;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); map[x][y]+=z; } printf("%d\n",Ford_fulkerson(1,m)); } }
NYOJ 323 && HDU 1532 && POJ 1273 Drainage Ditches (网络流之最大流入门)
原文地址:http://blog.csdn.net/u013050857/article/details/43372657