网络流模板——Dinic+ISAP

思路和刘汝佳白书上的一样,收藏一波~

ISAP:

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;
 
#define N 1000
#define INF 100000000
struct Edge
{
    int from,to,cap,flow;
};
 
struct ISAP
{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[N];
    bool vis[N];
    int d[N],cur[N];
    int p[N],num[N];//比Dinic算法多了这两个数组,p数组标记父亲结点,num数组标记距离d[i]存在几个
    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);
    }
 
    int Augumemt()
    {
        int x=t,a=INF;
        while(x!=s)//找最小的残量值
        {
            Edge&e=edges[p[x]];
            a=min(a,e.cap-e.flow);
            x=edges[p[x]].from;
        }
        x=t;
        while(x!=s)//增广
        {
            edges[p[x]].flow+=a;
            edges[p[x]^1].flow-=a;
            x=edges[p[x]].from;
        }
        return a;
    }
    void bfs()//逆向进行bfs
    {
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(t);
        d[t]=0;
        vis[t]=1;
        while(!q.empty())
        {
            int x=q.front();q.pop();
            int len=G[x].size();
            for(int i=0;i<len;i++)
            {
                Edge&e=edges[G[x][i]];
                if(!vis[e.from]&&e.cap>e.flow)
                {
                    vis[e.from]=1;
                    d[e.from]=d[x]+1;
                    q.push(e.from);
                }
            }
        }
    }
 
    int Maxflow(int s,int t)//根据情况前进或者后退,走到汇点时增广
    {
        this->s=s;
        this->t=t;
        int flow=0;
        bfs();
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
            num[d[i]]++;
        int x=s;
        memset(cur,0,sizeof(cur));
        while(d[s]<n)
        {
            if(x==t)//走到了汇点,进行增广
            {
                flow+=Augumemt();
                x=s;//增广后回到源点
            }
            int ok=0;
            for(int i=cur[x];i<G[x].size();i++)
            {
                Edge&e=edges[G[x][i]];
                if(e.cap>e.flow&&d[x]==d[e.to]+1)
                {
                    ok=1;
                    p[e.to]=G[x][i];//记录来的时候走的边,即父边
                    cur[x]=i;
                    x=e.to;//前进
                    break;
                }
            }
            if(!ok)//走不动了,撤退
            {
                int m=n-1;//如果没有弧,那么m+1就是n,即d[i]=n
                for(int i=0;i<G[x].size();i++)
                {
                    Edge&e=edges[G[x][i]];
                    if(e.cap>e.flow)
                        m=min(m,d[e.to]);
                }
                if(--num[d[x]]==0)break;//如果走不动了,且这个距离值原来只有一个,那么s-t不连通,这就是所谓的“gap优化”
                num[d[x]=m+1]++;
                cur[x]=0;
                if(x!=s)
                    x=edges[p[x]].from;//退一步,沿着父边返回
            }
        }
        return flow;
    }
};
int main()
{
//    freopen("t.txt","r",stdin);
    ISAP sap;
    while(cin>>sap.n>>sap.m)
    {
        for(int i=0;i<sap.m;i++)
        {
            int from,to,cap;
            cin>>from>>to>>cap;
            sap.addedge(from,to,cap);
        }
        cin>>sap.s>>sap.t;
        cout<<sap.Maxflow(sap.s,sap.t)<<endl;
    }
    return 0;
}
 
 

Dinic:

/* filename :network_flow_Dinic.cpp
 * author :AntiheroChen
 * Description :It's a Dinic solution for the network flow prblem.
 * Complexity :O(V^2*E) always below this.
 * Version :1.00
 * History :
 * 1)2012/02/29 first release.
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn=1000+5, bign=1000000000;
int M, n, m, source, sink, c[maxn][maxn], cnt[maxn];
 
 
/* The arc of the flow network.*/
struct Pool
{
    int next, t, c;
} edge[maxn*maxn<<1];
 
 
/* The point of the flow network.*/
struct Point
{
    int son, cur, pre, lim, d;
} a[maxn];
 
 
/* Prepare for the algorithm.*/
void initialize()
{
    M=1;
    memset(c, 0, sizeof (c));
    memset(a, 0, sizeof (a));
    memset(cnt, 0, sizeof (cnt));
}
 
 
/* Add an arc to the flow network.*/
void add(int x, int y, int z)
{
    edge[++M].t=y;
    edge[M].c=z;
    edge[M].next=a[x].son;//相当于pool的head数组
    a[x].son=M;
}
 
 
/* Read the data and make it the right format.*/
void input()
{
    scanf("%*s%*d%d%d%d%d", &n, &m, &source, &sink);
    initialize();
    int x, y, z;
    while (m--)
        scanf("%d%d%d", &x, &y, &z), c[x][y]+=z;
    for (int i=0; i<n; i++)
        for (int j=0; j<n; j++)
            if (c[i][j])add(i, j, c[i][j]), add(j, i, c[j][i]), c[j][i]=0;
}
 
 
int que[maxn], fi, la;
bool vis[maxn];
 
 
/* Build the hierarchical graph for the algorithm*/
bool build()
{
    memset(vis, 0, sizeof (vis));
    que[fi=la=0]=sink;//reverse
    a[sink].d=0, a[sink].cur=a[sink].son, vis[sink]=true;
    while (fi<=la)
    {
        int v=que[fi++];
        for (int now=a[v].son, u; u=edge[now].t, now; now=edge[now].next)
            if (edge[now^1].c&&!vis[u])//BFS来分层,这里和EK相同
            {//倒着BFS的话,当然引用的还是对侧边,即正向边
                a[u].d=a[v].d+1;//越向前标号渐大
                a[u].cur=a[u].son;//cur指向头
                vis[u]=true;//已遍历
                que[++la]=u;//入队
            }
        if (vis[source])return true;//层次图向前已经扩展到原点
    }
    return false;
}
 
 
/*Use the Dinic algorithm to calculate the max flow.*/
int MaxFlow()
{
    int u, v, now, ret=0;
    while (build())
    {
        a[u=source].lim=bign;
        while (true)
        {
            for (now=a[u].cur; v=edge[now].t, now; now=edge[now].next)//cur优化
                if (edge[now].c&&a[u].d==a[v].d+1)break;//找到了一个子节点属于层次图
            if (now)
            {
                a[u].cur=edge[now].next;//下一次从这一条边的下一条边开始dfs
                a[v].pre=now;//指向v的边的指针
                a[v].lim=min(a[u].lim, edge[now].c);///更新到此处为止流的上限
                if ((u=v)==sink)//如果已经找到了一条增广路(走到了尽头)
                ///注意这个地方借判断语句, 将u下移, 便于判断为否的时候回到上面进入下一层!
                {//进行增广
                    do
                    {
                        edge[a[u].pre].c-=a[sink].lim;
                        edge[a[u].pre^1].c+=a[sink].lim;//这两句和Edmonds-Karp是一样的,增广
                        u=edge[a[u].pre^1].t;//找前驱~!
                    } while (u!=source);
                    ret+=a[sink].lim;//增广完毕之后累加新找到的流
                }//否则(没走到尽头)继续向下DFS
            }
            else//没有子节点属于层次图
            {
                if (u==source)break;//已经退到了源,则已找到最大流,算法结束
                a[u].cur=now;//=0,此节点被废弃,子代亦然
                u=edge[a[u].pre^1].t;//根据反向边找到前驱~!
            }
        }
    }
    return ret;
}
 
 
int main()
{
    int total;
    scanf("%d", &total);
    while (total--)
    {
        input();
        printf("%d\n", MaxFlow());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Q755100802/article/details/82953811