• hdu 3549 Flow Problem(最大流 EK,sap)


    Problem Description

    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

    Input

    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

    Output

    For each test cases, you should output the maximum flow from source 1 to sink N.

    Sample Input

    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1

    Sample Output

    Case 1: 1
    Case 2: 2
    

    题意:首先给定有多少组测试数据,然后给定一个顶点数,边数,然后给定一个单向的流量,求从第一个点到最后一个点的最大流量。注意该流网络是有向图


    最大流基础题,可以ek或者sap算法;

    代码:

    ek算法

    #include
    #include
    #include
    #include
    using namespace std;
    int n,m;
    const int N=20;
    int map[N][N];
    int pre[N],vis[N];
    int bfs(int s,int t)
    {
        memset(pre,0,sizeof(pre));
        memset(vis,0,sizeof(vis));
        queueque;
        que.push(s);
        while(que.size())
        {
            int q=que.front();
            que.pop();
            if(q==t)
                return 1;
            for(int i=1;i<=t;i++)
            {
                if(map[q][i]&&!vis[i])
                {
                    pre[i]=q;
                    vis[i]=1;
                    que.push(i);
                }
            }
        }
        return 0;
    }
    int ek(int s,int t)
    {
        int min=0;
        while(bfs(s,t))
        {
            int minn=0xfffffff;
            for(int i=t;i!=s;i=pre[i])
            {
                if(minn>map[pre[i]][i])
                    minn=map[pre[i]][i];
            }
            min+=minn;
            for(int i=t;i!=s;i=pre[i])
            {
                map[pre[i]][i]-=minn;
                map[i][pre[i]]+=minn;
            }
        }
        return min;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int t=1;t<=T;t++)
        {
            scanf("%d%d",&n,&m);
            memset(map,0,sizeof(map));
            for(int i=1;i<=m;i++)
            {
                int a,b,w;
                scanf("%d%d%d",&a,&b,&w);
                map[a][b]+=w;
            }
            printf("Case %d: ",t);
            printf("%d\n",ek(1,n));
        }
    }


    sap

    #include
    #include
    #include
    using namespace std;
    const int N=20;
    int leve[N];
    int n,m;
    int map[N][N],gap[N],pre[N];
    int sap(int s,int t)
    {
        memset(pre,0,sizeof(pre));
        memset(gap,0,sizeof(gap));
        memset(leve,0,sizeof(leve));
        int u;
        int min=0;
        u=pre[s]=s;
        gap[0]=t;
        while(leve[s]map[pre[i]][i])
                            minn=map[pre[i]][i];
                    min+=minn;
                    for(int i=t;i!=s;i=pre[i])
                    {
                        map[pre[i]][i]-=minn;
                        map[i][pre[i]]+=minn;
                    }
                    u=s;
                }
            }
            else
            {
                int minleve=t;
                for(int i=1;i<=n;i++)
                     if(minleve>leve[i]&&map[u][i])
                         minleve=leve[i];
                gap[leve[u]]--;
                if(gap[leve[u]]==0)
                    break;
                leve[u]=minleve+1;
                gap[leve[u]]++;
                u=pre[u];
            }
        }
        return min;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int t=1;t<=T;t++)
        {
            scanf("%d%d",&n,&m);
            memset(map,0,sizeof(map));
            for(int i=1;i<=m;i++)
            {
                int a,b,w;
                scanf("%d%d%d",&a,&b,&w);
                map[a][b]+=w;
            }
            printf("Case %d: ",t);
            printf("%d\n",sap(1,n));
        }
    }
    
  • 相关阅读:
    金仓数据库KingbaseES ksql工具用户指南及参考--3. Ksql入门
    随想录一刷Day41——动态规划
    【JS】js给对象动态添加、设置、删除属性名和属性值
    论文中的小细节——为什么论文中总是写WX而不是XW?
    Java面试题
    Zookeeper实现分布式锁的原理。
    math_(函数&数列)极限的含义&误区和符号梳理/邻域&去心邻域&邻域半径
    Docker基本操作
    时代落在英伟达身上的是粒什么沙,国产GPU的机会又在哪?
    每日一库:lumberjack -- 日志轮换和管理
  • 原文地址:https://blog.csdn.net/stationinthemind/article/details/127862654