1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #define INF 0x3f3f3f3f
  11. using namespace std;
  12. int n,m;
  13. const int maxn=1005;
  14. int g[maxn][maxn];//容量
  15. int f[maxn][maxn];//流量
  16. bool vis[maxn];
  17. int pre[maxn];
  18. bool bfs(int s,int t){
  19. memset(pre,-1,sizeof pre);
  20. memset(vis,false,sizeof vis);
  21. queue<int> q;
  22. vis[s]=true;
  23. q.push(s);
  24. while(!q.empty()){
  25. int now=q.front();
  26. q.pop();
  27. for(int i=1;i<=n;i++){
  28. if(!vis[i]&&g[now][i]){
  29. vis[i]=true;
  30. pre[i]=now;
  31. if(i==t) return true; //找到汇点,即找到增广路。
  32. q.push(i);
  33. }
  34. }
  35. }
  36. return false;
  37. }
  38. int EK(int s,int t){
  39. int maxflow=0;
  40. int u,v;
  41. while(bfs(s,t)){
  42. int d=INF;
  43. v=t;
  44. while(v!=s){
  45. u=pre[v];
  46. if(d>g[u][v])
  47. d=g[u][v];
  48. v=u;
  49. }
  50. maxflow+=d;
  51. v=t;
  52. while(v!=s){
  53. u=pre[v];
  54. g[u][v]-=d;
  55. g[v][u]+=d;
  56. if(f[v][u]>0)
  57. f[v][u]-=d;
  58. else f[u][v]+=d;
  59. v=u;
  60. }
  61. }
  62. return maxflow;
  63. }
  64. int main()
  65. {
  66. //int s,t;
  67. int u,v,c;
  68. while(~scanf("%d%d",&m,&n)){
  69. memset(f,0,sizeof f);
  70. memset(g,0,sizeof g);
  71. for(int i=1;i<=m;i++){
  72. scanf("%d%d%d",&u,&v,&c);
  73. g[u][v]+=c;
  74. }
  75. printf("%d\n",EK(1,n));
  76. }
  77. return 0;
  78. }

网络流--最大流--EK模板的更多相关文章

  1. 网络流--最大流ek模板

    标准大白书式模板,代码简单但由于效率并不高,所以并不常用,就是这样 #include<stdio.h> #include<string.h> #include<queue ...

  2. (通俗易懂小白入门)网络流最大流——EK算法

    网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...

  3. 网络流--最大流dinic模板

    标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...

  4. 网络流 最大流SAPkuangbin模板

    hdu 1532 求1~n的最大流 #include<stdio.h> #include<string.h> #include<algorithm> #includ ...

  5. 网络流-最大流 Dinic模板

    #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...

  6. 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...

  7. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

  8. HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

随机推荐

  1. C语言中 sinx cosx 的用法

    #include<stdio.h> #include<math.h> int main() {     double pi=acos(-1.0);     double ang ...

  2. Typora+PicGo+GitHub实现md自带图床效果

    1 GitHub创建作为图床的仓库 1.1 在GitHub中创建一个仓库 注意仓库要是public的,不然上传的图片还是无法使用的.如果不知道怎么创建仓库,可以百度一下. 1.2 在GitHub生成一 ...

  3. GitHub+PicGo构建免费图床及其高效使用

    搭建免费图床全过程! 一.搭建缘由 一开始搭建博客,避免不了要用许多图片,最初使用七牛云来做博客图床,但是后来发现,七牛云只有30天的临时域名,hhhhhhh,果然啊,天下就没有免费的好事啊~后来就发 ...

  4. NumPy学习2:创建数组

    1.使用array创建数组 b = array([2, 3, 4])print bprint b.dtype 2.把序列转化为数组 b = array( [ (1.5,2,3), (4,5,6) ] ...

  5. python3(二十七)property

    """ """ __author__ = 'shaozhiqi' # 绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单, # 但是, ...

  6. vue2.x学习笔记(十三)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12595860.html. 组件的注册 注册组件有一些规范约定与注意事项. 组件名的命名规范 在注册一个组件的时候, ...

  7. Element里el-badge在el-tab里视图不被渲染问题

    我们发现:el-badge绑定的变量是有数据的,但是界面上就是不渲染. 这个时候执行getTodo发现数据已经打印出来,当是视图未发送变化.于是查阅资料:vm.$forceUpdate()示例:迫使 ...

  8. 虎符ctf-MISC-奇怪的组织(看完官方题解,找到了)

    一道取证题,一整场比赛,基本就死磕了这一题 写的很乱,因为当时的思维就是那么乱,完全没有注意到出题人的提示, 还没做出来,没有找到关键key 那个人的real name 文档:虎符.note链接:ht ...

  9. 用functools.lru_cache实现Python的Memoization

    现在你已经看到了如何自己实现一个memoization函数,我会告诉你,你可以使用Python的functools.lru_cache装饰器来获得相同的结果,以增加方便性. 我最喜欢Python的原因 ...

  10. Apache jena SPARQL endpoint及推理

    一.Apache Jena简介 Apache Jena(后文简称Jena),是一个开源的Java语义网框架(open source Semantic Web Framework for Java),用 ...