码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SPOJ 4110 Fast Maximum Flow (最大流模板)


    题目大意:

    无向图,求最大流。

    算法讨论:

    Dinic可过。终于我的常数还是太大。以后要注意下了。

     1 #include 
     2 #include 
     3 #include 
     4 #include 
     5 #include 
     6 #include 
     7 using namespace std;
     8 typedef long long ll;
     9  
    10 struct MF{
    11   static const int N = 5000 + 5;
    12   static const int M = 60000 + 5;
    13   static const ll oo = 10000000000000LL;
    14     
    15   int n, m, s, t, tot, tim;
    16   int first[N], next[M];
    17   int u[M], v[M], cur[N], vi[N];
    18     ll cap[M], flow[M], dis[N];
    19   int que[N + N];
    20   
    21   void Clear(){
    22     tot = 0;tim = 0;
    23        for(int i = 1; i <= n; ++ i) first[i] = -1;
    24   }
    25   void Add(int from, int to, ll cp, ll flw){
    26     u[tot] = from; v[tot] = to; cap[tot] = cp; flow[tot] = flw;
    27     next[tot] = first[u[tot]];
    28     first[u[tot]] = tot;
    29     ++ tot;
    30   }
    31   bool bfs(){
    32       ++ tim;
    33     dis[s] = 0;vi[s] = tim;
    34     
    35     int head, tail;
    36     head = tail = 1;
    37     que[head] = s;
    38     while(head <= tail){
    39         for(int i = first[que[head]]; i != -1; i = next[i]){
    40             if(vi[v[i]] != tim && cap[i] > flow[i]){
    41                 vi[v[i]] = tim;
    42                 dis[v[i]] = dis[que[head]] + 1;
    43                     que[++ tail] = v[i];
    44                 }
    45             }
    46             ++ head;
    47         }
    48        return vi[t] == tim;
    49   }    
    50   ll dfs(int x, ll a){
    51     if(x == t || a == 0) return a;
    52     ll flw = 0, f;
    53     int &i = cur[x];
    54     for(i = first[x]; i != -1; i = next[i]){
    55       if(dis[x] + 1 == dis[v[i]] && (f = dfs(v[i], min(a, cap[i]-flow[i]))) > 0){
    56         flow[i] += f; flow[i^1] -= f;
    57           a -= f; flw += f;
    58           if(a == 0) break;
    59         }
    60       }
    61     return flw;
    62   }
    63     ll MaxFlow(int s, int t){
    64     this->s = s;this->t = t;
    65     ll flw = 0;
    66     while(bfs()){
    67       for(int i = 1; i <= n; ++ i) cur[i] = 0;
    68       flw += dfs(s, oo);
    69     }
    70     return flw;
    71   }
    72 }Net;
    73 int n, m;
    74  
    75 int main(){
    76     int x, y;
    77     ll z;
    78     scanf("%d%d", &n, &m);
    79     Net.n = n;
    80     Net.Clear();
    81     for(int i = 1; i <= m; ++ i){
    82         scanf("%d%d%lld", &x, &y, &z);
    83         Net.Add(x, y, z, 0);
    84         Net.Add(y, x, z, 0);
    85     }    
    86     printf("%lld\n", Net.MaxFlow(1,Net.n));
    87     return 0;
    88 }

    SPOJ 4110

  • 相关阅读:
    【Java】表白墙终章-飞流直下的“甜言蜜语”-瀑布流式布局
    并发调用deepseek API,构建多轮对话数据
    2022数学建模国赛降至,整理了一些很不错的在线网站分享一下
    【Qt6】列表模型——便捷类型
    ANSYS mechanical如何在Workbench环境中使用高性能计算
    三面头条+四面阿里+五面腾讯拿offer分享面经总结,最终入职阿里
    Java开发学习----SpringMVC设置请求映射路径
    Python:温度转换(摄氏度与华氏度)
    C++基于多设计模式下的同步&异步日志系统day3
    el-form那些事
  • 原文地址:https://blog.csdn.net/weixin_71792169/article/details/127857039
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号