码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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));
        }
    }
    
  • 相关阅读:
    创建数字藏品艺术平台需要多少成本
    【SA8295P 源码分析】105 - QNX MISC分区读写、切换A/B启动槽、读取开机次数命令 swdl_utils 介绍 及 祼分区读写 代码实现
    月薪9K和年薪30W的职位,有什么区别?
    微信小程序点单左右联动的效果实现
    报错解决:libcudart.so和libprotobuf.so链接库未找到
    JAVA面试题2:什么是面向对象?
    RTX30系列linux+docker容器的GPU配置(tensorflow-gpu==1.15~2.x、tensorrt 7、cuda、cudnn)
    药品大数据公司都有哪些?
    在Java中比较对象修改字段并忽略指定字段的方法
    No6-3.从零搭建spring-cloud-alibaba微服务框架,实现资源端用户认证与授权等(三,no6-3)
  • 原文地址:https://blog.csdn.net/stationinthemind/article/details/127862654
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号