1. 程式人生 > >最大流 && 最小費用最大流模板

最大流 && 最小費用最大流模板

() 鏈接 處理 article 最大流 最短路徑 cos 一次 bfs

模板從 這裏 搬運,鏈接博客還有很多網絡流題集題解參考。

最大流模板 ( 可處理重邊 )

技術分享圖片
struct Edge  
{  
    Edge(){}  
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}  
    int from,to,cap,flow;  
};  
  
struct Dinic  
{  
    int n,m,s,t;            //結點數,邊數(包括反向弧),源點與匯點編號  
    vector<Edge> edges;     //
邊表 edges[e]和edges[e^1]互為反向弧 vector<int> G[maxn]; //鄰接表,G[i][j]表示結點i的第j條邊在e數組中的序號 bool vis[maxn]; //BFS使用,標記一個節點是否被遍歷過 int d[maxn]; //d[i]表從起點s到i點的距離(層次) int cur[maxn]; //cur[i]表當前正訪問i節點的第cur[i]條弧 void init(int n,int s,int t) {
this->n=n,this->s=s,this->t=t; 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) ); 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;//用來保存節點編號的 Q.push(s); d[s]=0; vis[s]=true; 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]=true; d[e.to] = d[x]+1; Q.push(e.to); } } } return vis[t]; } //a表示從s到x目前為止所有弧的最小殘量 //flow表示從x到t的最小殘量 int DFS(int x,int a) { if(x==t || a==0)return a; int flow=0,f;//flow用來記錄從x到t的最小殘量 for(int& i=cur[x]; i<G[x].size(); i++) { 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; } int Maxflow() { int flow=0; while(BFS()) { memset(cur,0,sizeof(cur)); flow += DFS(s,INF); } return flow; } }DC;
View Code

最小費用最大流模板

技術分享圖片
struct Edge  
{  
    int from,to,cap,flow,cost;  
    Edge(){}  
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}  
};  
  
struct MCMF  
{  
    int n,m,s,t;  
    vector<Edge> edges;  
    vector<int> G[maxn];  
    bool inq[maxn];     //是否在隊列  
    int d[maxn];        //Bellman_ford單源最短路徑  
    int p[maxn];        //p[i]表從s到i的最小費用路徑上的最後一條弧編號  
    int a[maxn];        //a[i]表示從s到i的最小殘量  
  
    //初始化  
    void init(int n,int s,int t)  
    {  
        this->n=n, this->s=s, this->t=t;  
        edges.clear();  
        for(int i=0;i<n;++i) G[i].clear();  
    }  
  
    //添加一條有向邊  
    void AddEdge(int from,int to,int cap,int cost)  
    {  
        edges.push_back(Edge(from,to,cap,0,cost));  
        edges.push_back(Edge(to,from,0,0,-cost));  
        m=edges.size();  
        G[from].push_back(m-2);  
        G[to].push_back(m-1);  
    }  
  
    //求一次增廣路  
    bool BellmanFord(int &flow, int &cost)  
    {  
        for(int i=0;i<n;++i) d[i]=INF;  
        memset(inq,0,sizeof(inq));  
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;  
        queue<int> Q;  
        Q.push(s);  
        while(!Q.empty())  
        {  
            int u=Q.front(); Q.pop();  
            inq[u]=false;  
            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)  
                {  
                    d[e.to]= d[u]+e.cost;  
                    p[e.to]=G[u][i];  
                    a[e.to]= min(a[u],e.cap-e.flow);  
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }  
                }  
            }  
        }  
        if(d[t]==INF) return false;  
        flow +=a[t];  
        cost +=a[t]*d[t];  
        int u=t;  
        while(u!=s)  
        {  
            edges[p[u]].flow += a[t];  
            edges[p[u]^1].flow -=a[t];  
            u = edges[p[u]].from;  
        }  
        return true;  
    }  
  
    //求出最小費用最大流  
    int Min_cost()  
    {  
        int flow=0,cost=0;  
        while(BellmanFord(flow,cost));  
        return cost;  
    }  
}MM;  
View Code

網絡流的知識可以參考《挑戰程序設計競賽Ⅱ》 or 以下鏈接

鏈接、鏈接Ⅱ、鏈接Ⅲ

最大流 && 最小費用最大流模板