洛谷p3376 https://www.luogu.com.cn/problem/P3376

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cstring>
  6. #define INF 0x3f3f3f3f
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 1000000 + 5;
  10. int n,m,be,en;
  11. queue<int>q;
  12. int to[maxn];
  13. int cost[maxn];
  14. int head[maxn];
  15. int nex[maxn];
  16. int cnt;
  17. void add(int x,int y,int z)
  18. {
  19. cnt++;
  20. nex[cnt]=head[x]; //先把指针指向上次的位置
  21. head[x]=cnt; //然后把头指向自己
  22. to[cnt]=y;
  23. cost[cnt]=z;
  24. }
  25. int level[maxn];
  26. bool bfs() //深度打表(分层)
  27. {
  28. memset(level,-1,sizeof(level));
  29. level[be]=0;
  30. q.push(be);
  31. while(!q.empty())
  32. {
  33. int u=q.front();
  34. q.pop();
  35. for(int i=head[u];i!=-1;i=nex[i])
  36. {
  37. int v=to[i];
  38. if(cost[i]!=0&&level[v]==-1) //流量大于0并且未被访问
  39. {
  40. level[v]=level[u]+1;
  41. q.push(v);
  42. }
  43. }
  44. }
  45. if(level[en]!=-1) return 1;
  46. else return 0;
  47. }
  48. int dfs(int u,int flow) //dfs找最大流 对于源点,流入它的最大流量是无限大 flow最大可行流 ret剩余可行流量
  49. {
  50. if(u==en) return flow;
  51. int ret=flow;
  52. for(int i=head[u];i!=-1;i=nex[i])
  53. {
  54. if(ret<=0) break;
  55. int v=to[i];
  56. if(cost[i]!=0&&level[u]+1==level[v])
  57. {
  58. int k=dfs(v,min(ret,cost[i])); //把能流的都给下一个点
  59. ret-=k;cost[i]-=k;cost[i^1]+=k; //i^1就是反向边
  60. }
  61. }
  62. return flow-ret; //最大可行流减去剩余可行流就是实际流量
  63. }
  64. int dinic()
  65. {
  66. int ans=0;
  67. while(bfs()==true) //还能分层就不断增广
  68. {
  69. ans+=dfs(be,INF);
  70. }
  71. return ans;
  72. }
  73. int main()
  74. {
  75. scanf("%d%d%d%d",&n,&m,&be,&en);
  76. memset(head,-1,sizeof(head));
  77. int x,y,z;
  78. cnt=-1; //从0开始 0 1 一对 2 3 一对
  79. for(int i=1;i<=m;i++)
  80. {
  81. scanf("%d%d%d",&x,&y,&z);
  82. add(x,y,z); add(y,x,0);
  83. }
  84. printf("%d\n",dinic());
  85. return 0;
  86. }

网络流(dinic算法)的更多相关文章

  1. poj 1459 Power Network : 最大网络流 dinic算法实现

    点击打开链接 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20903   Accepted:  ...

  2. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  3. 网络流(dinic算法)

    网络最大流(dinic) 模型 在一张图中,给定一个源点s,给定汇点t,点之间有一些水管,每条水管有一个容量,经过此水管的水流最大不超过容量,问最大能有多少水从s流到t(s有无限多的水). 解法 di ...

  4. 网络流Dinic算法

    我的模板 例题: https://vjudge.net/problem/HDU-4280 struct Edge { int lst; int from; int to; int cap; int f ...

  5. 高效的网络流dinic算法模版

    #include <cstring> #include <algorithm> #include <vector> #define Maxn 120010 #def ...

  6. POJ 3281 [网络流dinic算法模板]

    题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...

  7. 网络流Dinic算法模板 POJ1273

    这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...

  8. POJ 3281 网络流dinic算法

    B - Dining Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  9. [知识点]网络流之Dinic算法

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html      ...

  10. 网络流入门—用于最大流的Dinic算法

    "网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...

随机推荐

  1. redis高级命令3哨兵模式

    redis的哨兵模式 现在我们在从服务器1.222上让该从服务器作为哨兵 首先将redis安装包文件下的sentinel.conf文件复制到/usr/local/redis/etc目录下 然后修改se ...

  2. 小师妹学JVM之:JIT中的PrintCompilation

    目录 简介 PrintCompilation 分析PrintCompilation的结果 总结 简介 上篇文章我们讲到了JIT中的LogCompilation,将编译的日志都收集起来,存到日志文件里面 ...

  3. caffe训练数据流程

    cifar10训练实例 1. 下载数据 # sudo sh data/cifar10/get_cifar10.sh 2. 转换数据格式为lmdb # sudo sh examples/cifar10/ ...

  4. Python HTTP Server (Simples)

    Simple HTTP Server 适合临时开发调试web 使用, 直接当前项目下使用python命令快速起一个http server python2 python -m SimpleHTTPSer ...

  5. 语言模型 N-gram 与其平滑方法推导

    N-gram N-gram 作为一个名词表示的是一个给定文本/音频样本中有n项(音素,音节,字母,单词)的一个连续序列. 数学表达 N-gram 模型表示的是当前这个 word \(w_i\) 依赖于 ...

  6. could not resolve property(无法解析属性)

    could not resolve property(无法解析属性) 顾名思义在写hql语句的时候,属性写错了! 请检查大小写,是实体类的,不是数据库表的! 一个一个检查,仔细看!

  7. js写一个简单的九九乘法表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Django---drf第一天

    目录 1 序列化组件介绍 2 简单使用 3 序列化类的字段类型 4 序列化字段选项 5 序列化组件修改数据 6 read_only和write_only 7查询所有 8 新增数据 9 删除一个数据 1 ...

  9. day49 数据库终章

    目录 一.pymysql补充 二.数据库补充 1 视图(了解) 2 触发器(了解) 3 事务 4 存储过程(了解) 5 函数 6 流程控制 7 索引 8 b+树 9 聚集索引(primary key) ...

  10. shell进阶篇之数组应用案例

    数组中可以存放多个值. Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小. 与大部分编程语言类似,数组元素的下标由0开始. Shell 数组用括号来表示,元素用"空格 ...