图论总结(9)网络流问题

来源:互联网 发布:c语言浮点数表示方法 编辑:程序博客网 时间:2024/06/16 05:42

说实话网络流学得很浅,只能做点水题,没时间练题了。

下面总结下几类题型。

一.最大流问题

概述问题就是一个有向图,每条边都有个容量,给你源点s和汇点t,问你从s到t的最大流量是多少。

用增广路算法可以解决

先求出每条边对应反向边的残量,构造出残量网络,若残量网络中有从s到t的路径,则s到t存在增广路(简单理解为可以是流量变大的路径),显然但残量网络中不存在增广路时有最大流量。

EdmondKarp算法

模板:

const int INF=0X3f3f3f3f;struct Edge{int from,to,cap,flow;Edge(int fr,int t,int c,int fl):from(fr),to(t),cap(c),flow(fl){}};struct EdmondKarp{int n,m;vector<Edge>edges;vector<int>G[maxn];int a[maxn];int p[maxn];void init(int n){for(int i=0;i<n;i++)G[i].clear();edges.clear();}void AddEdge(int from,int to,int cap){edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1);  }int Maxflow(int s,int t){int flow=0;for(;;){memset(a,0,sizeof(a));memset(p,0,sizeof(p));queue<int>q;q.push(s);a[s]=INF;while(!q.empty()){int u=q.front();q.pop();for(int i=0;i<G[u].size();i++){Edge &e=edges[G[u][i]];if(p[e.to]==0&&e.cap>e.flow&&e.to!=s){p[e.to]=G[u][i];a[e.to]=min(a[u],e.cap-e.flow);q.push(e.to);}}if(a[t])break;}if(!a[t])break;for(int u=t;u!=s;u=edges[p[u]].from){edges[p[u]].flow+=a[t];edges[p[u]^1].flow-=a[t];//反向边}flow+=a[t];}return flow;}};

Dinc算法:

EdmondKarp算法太慢,比赛一般不用,紫书中推荐了两种算法,Dinic和ISPA,个人更喜欢Dinic,ISPA我反正比赛用不到了,时间问题先只给出Dinic算法

Dinic比EdmandKarp算法的改进在于先用bfs()求出了各个点到源点的层次d(u);在用dfs()找满足d(v)=d(u)+1的增广路。

模板:

const int INF=0X3f3f3f3f;struct Edge{int from,to,cap,flow;Edge(int fr,int t,int c,int fl):from(fr),to(t),cap(c),flow(fl){}};struct Dinic{int n,m,s,t;vector<Edge>edges;vector<int>G[maxn];int vis[maxn],p[maxn],d[maxn],cur[maxn];void init(int n){for(int i=1;i<=n;i++)G[i].clear();edges.clear();}void AddEdge(int from,int to,int cap){edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));int m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1); }bool bfs(){memset(vis,0,sizeof(vis));queue<int>q;d[s]=0;vis[s]=1;q.push(s);while(!q.empty()){int u=q.front();q.pop();for(int i=0;i<G[u].size();i++){Edge& e=edges[G[u][i]];if(!vis[e.to]){vis[e.to]=1;d[e.to]=d[u]+1;q.push(e.to);}}}return vis[t];}int dfs(int x,int a){if(x==t||a==0)return a;int flow=0,f;for(int &i=cur[x];i<G[x].size();i++){Edge& e=edges[G[x][i]];if(d[e.to]=d[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow))>0)){a-=f;e.flow+=f;edges[G[x][i]^1].flow-=f;flow+=f;if(a==0)break;}}return flow;}int Maxflow(int s,int t){this->s=s;this->t=t;int flow=0;while(bfs()){flow+=dfs(s,INF);}return flow;} };
二.最小费用最大流问题

每条边加了一个费用,问在总流量最大的前提下最小费用 为多少。

只需要将Edmond算中求增广路的方法换为BellmanFord算法求出最小费用的路径就行了。

bool Bellmanford(int s,int t,int &flow,int cost){for(int i=1;i<=n;i++)d[i]=INF;memset(vis,0,sizeof(vis));d[s]=0;vis[s]=1;p[s]=0;a[s]=INF;queue<int>q;q.push(s);while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=0;i<G[u].size();i++){Edge& e=edges[G[u][i]];if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){p[e.to]=G[u][i];d[e.to]=d[u]+e.cost;a[e.to]=min(a[u],e.cap-e.flow);if(!vis[e.to]){q.push(e.to);vis[e.to]=1;}}}}if(d[t]==INF)return false;flow+=a[t];cost+=a[t]*d[t];for(int u=t;u!=s;u=edges[p[u]].from){edges[p[u]].flow+=a[t];edges[p[u]^1].flow-=a[t];}return true;}int MincostMaxflow(int s,int t,int &cost){int flow=0;cost=0;while(Bellmanford(s,t,flow,cost));return flow ;}
还有些小的东西就不说了。

二分图单独总结吧,毕竟东西还有点多。






原创粉丝点击