8.8 Dinic模板学习心得

来源:互联网 发布:windows u盘安装 编辑:程序博客网 时间:2024/06/05 21:05

(这几天有点废,不能这样了)

dinic是网络流模板,而且是最大流的跑的比较的一种写法,属于考试必背的东西,,话不多说,直接上lrj版代码

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <memory.h>#include <math.h>#include <queue>#include <stack>#include <map>#include <vector>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <float.h>using namespace std;struct Dinic{    int n,m,s,t;    vector <Edge> edges;;    vector <int> G[maxn];    bool vis[maxn];    int d[maxn];    int cur[maxn];        bool BFS(){        memset(vis,0,sizeof(vis));        queue <int> Q;        Q.push(s);        d[s]=0;        vis[s]=1;        while(!Q.empty()){            int x=Q.front();            Q.pop();            for(int i=0;i<G[x].size();i++){                Edge& e=edges[G[x][i]];                if(!vis[e.to]&&e.cap>e.flow){//一定要在残量找层次图,切记                    vis[e.to]=1;                    d[e.to]=d[x]+1;                    Q.push(e.to);                }            }        }        return vis[t];    }    int DFS(int x,int a){        if(x==t||a==0) return a;//a是当前剩余的流量,为0就跑不了了         int flow=0,f;        for(int& i=cur[x];i<G[x].size();i++){//cur是当前弧优化             Edge& e=edges[G[x][i]];            if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e,flow)))>0){                e.flow+=f;                edges[G[x][i]^1].flow-=f;//就是反方向的边,位运算优化                 flow+=f;                a-=f;                if(a==0)                    break;            }        }        return flow;    }    //bfs分层,dfs在层次图上多路增广,跑得快     int Maxflow(int s,int t){        this->s=s;this->t=t;        int flow=0;        while(BFS()){            memset(cur,0,sizeof(cur));            flow+=DFS(s,INF);        }        return flow;    }};

这里面也没有什么难懂的,关键的点在注释中已经标明了,背一背就好了嘛ヾ(≧▽≦*)o

0 0
原创粉丝点击