• 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));
        }
    }
    
  • 相关阅读:
    IDM(Internet Download Manager)2024免激活绿色版下载
    Python 算法:感受算法的小小魅力和复杂度的计算
    从 160 万到 1.5 亿美元 ,开源软件迎来融资热潮
    Coredump
    SpringBoot携带Jre绿色部署项目_免安装Jdk[Linux服务器]
    postman接口测试中文汉化教程
    请求数据时,后端返回的状态码
    Java多线程第十三篇--盘一盘晕头转向的Runnable、Callable、Future、RunnableFuture、FutureT
    C++笔记 03
    Java HashMap源码学习
  • 原文地址:https://blog.csdn.net/stationinthemind/article/details/127862654