zoukankan      html  css  js  c++  java
  • Drainage Ditches---hdu1532(最大流, 模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532

    最大流模板题;

    EK:(复杂度为n*m*m);

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define INF 0xfffffff
    #define N 220
    int maps[N][N], pre[N], ans;
    bool bfs(int s, int e)
    {
        memset(pre, 0, sizeof(pre));
        queue<int>Q;
        Q.push(s);
        while(Q.size())
        {
            int i = Q.front();
            Q.pop();
            if(i == e)
                return true;
            for(int j=1; j<=e; j++)
            {
                if(pre[j]==0 && maps[i][j] > 0)
                {
                    pre[j] = i;
                    Q.push(j);
                }
            }
        }
        return false;
    }
    void EK(int s, int e)
    {
        while(bfs(s, e))
        {
            int Min = INF;
            for(int i=e; i!=s; i=pre[i])
                Min=min(maps[pre[i]][i], Min);
             for(int i=e; i!=s; i=pre[i])
             {
                 maps[pre[i]][i]-=Min;
                 maps[i][pre[i]]+=Min;
             }
             ans+=Min;
        }
    }
    int main()
    {
        int n, m, x, y,c;
        while(scanf("%d%d", &m, &n)!=EOF)
        {
            memset(maps, 0, sizeof(maps));
            for(int i=1; i<=m; i++)
            {
                scanf("%d%d%d", &x, &y, &c);
                maps[x][y] += c;
            }
            ans = 0;
            EK(1, n);
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code

    Dinic:(复杂度为n*n*m)

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    #define N 220
    #define INF 0xfffffff
    
    int n, ans, Head[N], cnt, Layer[N];
    struct Edge
    {
        int v, flow, next;
    } e[2*N];
    
    void Add(int u, int v, int flow)
    {
        e[cnt].v = v;
        e[cnt].flow = flow;
        e[cnt].next = Head[u];
        Head[u] = cnt++;
    }
    bool bfs(int S, int E)
    {
        memset(Layer, 0, sizeof(Layer));
        Layer[S] = 1;
        queue<int>Q;
        Q.push(S);
        int p, q;
        while(!Q.empty())
        {
            p = Q.front();
            Q.pop();
            if(p == E)return true;
            for(int i=Head[p]; i!=-1; i=e[i].next)
            {
                q = e[i].v;
                if(!Layer[q] && e[i].flow)
                {
                    Layer[q] = Layer[p]+1;
                    Q.push(q);
                }
            }
        }
        return false;
    }
    int dfs(int u, int MaxFlow, int E)
    {
        if(u == E)return MaxFlow;
        int  uflow=0;
        for(int i=Head[u]; i!=-1; i=e[i].next)
        {
            int v = e[i].v;
            if(Layer[v]==Layer[u]+1 && e[i].flow)
            {
                int flow = min(e[i].flow, MaxFlow - uflow);
                flow = dfs(v, flow, E);
    
                e[i].flow -= flow;
                e[i^1].flow += flow;
                uflow += flow;
                if(uflow==MaxFlow)break;
            }
        }
        if(uflow==0)
            Layer[u]=0;
        return uflow;
    }
    void Dinic()
    {
        while(bfs(1, n))
        {
            ans+=dfs(1, INF, n);
        }
    }
    int main()
    {
        int a, b, flow, m;
        while(scanf("%d%d", &m, &n)!=EOF)
        {
            memset(Head, -1, sizeof(Head));
            cnt = 0;
            for(int i=1; i<=m; i++)
            {
                scanf("%d%d%d", &a, &b, &flow);
                Add(a, b, flow);
                Add(b, a, 0);
            }
            ans = 0;
            Dinic();
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Redis配置文件详解
    linux系统配置Apache虚拟主机实例
    nginx File not found 错误分析与解决方法
    svn配置使用
    linux下svn命令使用大全
    Kendo UI For ASP.NET MVC项目资源
    ReSharper 配置及用法
    SQL判断某列中是否包含中文字符、英文字符、纯数字 (转)
    Visual Studio最好用的快捷键
    19个必须知道的Visual Studio快捷键
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4803019.html
Copyright © 2011-2022 走看看