Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 8199    Accepted Submission(s): 3814

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
 
题意:给一个有向图,求源点为1汇点为n的最大流
 
思路:裸题,ISAP。。原来之前自己写的模版有点疏漏了。。
 
  1. #include <vector>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define FOR(i,n) for(i=1;i<=(n);i++)
  6. using namespace std;
  7. const int INF = 1e9;
  8. const int N = ;
  9.  
  10. struct Edge{
  11. int from,to,cap,flow;
  12. };
  13.  
  14. struct ISAP{
  15. int n,m,s,t;
  16. int p[N],num[N];
  17. vector<Edge> edges;
  18. vector<int> G[N];
  19. bool vis[N];
  20. int d[N],cur[N];
  21. void init(int _n,int _m)
  22. {
  23. n=_n; m=_m;
  24. int i;
  25. edges.clear();
  26. FOR(i,n)
  27. {
  28. G[i].clear();
  29. d[i]=INF;
  30. }
  31. }
  32. void AddEdge(int from,int to,int cap)
  33. {
  34. edges.push_back((Edge){from,to,cap,});
  35. edges.push_back((Edge){to,from,,});
  36. m = edges.size();
  37. G[from].push_back(m-);
  38. G[to].push_back(m-);
  39. }
  40. bool BFS()
  41. {
  42. memset(vis,,sizeof(vis));
  43. queue<int> Q;
  44. Q.push(t);
  45. d[t]=;
  46. vis[t]=;
  47. while(!Q.empty())
  48. {
  49. int x = Q.front(); Q.pop();
  50. for(unsigned i=;i<G[x].size();i++)
  51. {
  52. Edge& e = edges[G[x][i]^];
  53. if(!vis[e.from] && e.cap>e.flow)
  54. {
  55. vis[e.from]=;
  56. d[e.from] = d[x]+;
  57. Q.push(e.from);
  58. }
  59. }
  60. }
  61. return vis[s];
  62. }
  63. int Augment()
  64. {
  65. int x=t, a=INF;
  66. while(x!=s)
  67. {
  68. Edge& e = edges[p[x]];
  69. a = min(a,e.cap-e.flow);
  70. x = edges[p[x]].from;
  71. }
  72. x = t;
  73. while(x!=s)
  74. {
  75. edges[p[x]].flow+=a;
  76. edges[p[x]^].flow-=a;
  77. x=edges[p[x]].from;
  78. }
  79. return a;
  80. }
  81. int Maxflow(int _s,int _t)
  82. {
  83. s=_s; t=_t;
  84. int flow = , i;
  85. BFS();
  86. if(d[s]>=n) return ;
  87. memset(num,,sizeof(num));
  88. memset(p,,sizeof(p));
  89. FOR(i,n) if(d[i]<INF) num[d[i]]++;
  90. int x=s;
  91. memset(cur,,sizeof(cur));
  92. while(d[s]<n)
  93. {
  94. if(x==t)
  95. {
  96. flow+=Augment();
  97. x=s;
  98. }
  99. int ok=;
  100. for(unsigned i=cur[x];i<G[x].size();i++)
  101. {
  102. Edge& e=edges[G[x][i]];
  103. if(e.cap>e.flow && d[x]==d[e.to]+)
  104. {
  105. ok=;
  106. p[e.to]=G[x][i];
  107. cur[x]=i;
  108. x=e.to;
  109. break;
  110. }
  111. }
  112. if(!ok)
  113. {
  114. int m=n-;
  115. for(unsigned i=;i<G[x].size();i++)
  116. {
  117. Edge& e=edges[G[x][i]];
  118. if(e.cap>e.flow) m=min(m,d[e.to]);
  119. }
  120. if(--num[d[x]]==) break;
  121. num[d[x]=m+]++;
  122. cur[x]=;
  123. if(x!=s) x=edges[p[x]].from;
  124. }
  125. }
  126. return flow;
  127. }
  128. };
  129.  
  130. ISAP isap;
  131.  
  132. void run()
  133. {
  134. int n,m,u,v,c;
  135. scanf("%d%d",&n,&m);
  136. isap.init(n,m);
  137. while(m--)
  138. {
  139. scanf("%d%d%d",&u,&v,&c);
  140. isap.AddEdge(u,v,c);
  141. //isap.AddEdge(v,u,c);
  142. }
  143. static int cas = ;
  144. printf("Case %d: %d\n",cas++,isap.Maxflow(,n));
  145. }
  146.  
  147. int main()
  148. {
  149. freopen("case.txt","r",stdin);
  150. int _;
  151. scanf("%d",&_);
  152. while(_--)
  153. run();
  154. return ;
  155. }
 

HDU 3549 Flow Problem (最大流ISAP)的更多相关文章

  1. HDU 3549 Flow Problem (dinic模版 && isap模版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...

  2. hdu - 3549 Flow Problem (最大流模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...

  3. hdu 3549 Flow Problem (最大流)

    裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...

  4. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

  5. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  6. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  7. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  8. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  9. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

随机推荐

  1. 使用ZipArchive解压

    本文转载至 http://www.apkbus.com/forum.php?mod=viewthread&tid=131390&extra=page%3D1 qqpk360 该用户从未 ...

  2. Django的标准库django.contrib包介绍(转)

    Django.contrib是啥? 1.它是一个强大的功能包,是Django的标准库.2.Django的标准库存放在 django.contrib 包中.每个子包都是一个独立的附加功能包. 这些子包一 ...

  3. android菜鸟学习笔记9----Activity(二)

    关于Activity的生命周期: 下面是Activity整个生命周期中,状态发生变化时所回调的方法,它们对应着Activity完整的生命过程. void  onCreate(Bundle savedI ...

  4. 各种jar包下方法的使用

    commons-codec-1.6.jar: DigestUtils.md5Hex(String str); httpclient-4.2.2.jar: HttpClient client=new D ...

  5. PYTHON调用C接口(基于Ctypes)实现stein算法最大公约数的计算

    相关环境配置 mingw,选择相应的32位.64位的版本,主要用于编译动态链接库dll文件,可用vs替代,这里我选择轻量级的mingw windows64位地址:https://sourceforge ...

  6. HBase存储方案设计

    需求描述 将数据记录持久化存储在HBase中,需要支持如下功能: 支持高吞吐量读写操作,实时采集10,000条/秒: 支持动态添加字段: 支持服务端过滤: 支持部分字段修改. 设计方案 按列存储 优点 ...

  7. 20145239杜文超 实验五 Java网络编程

    20145239 实验五 Java网络编程 实验内容 组队,一人服务器,一人客户端. 下载加解密代码,先编译运行代码,一人加密一人解密,适当修改代码. 然后集成代码,一人加密后通过TCP发送,加密使用 ...

  8. <J2EE学习笔记>续上次Servlet部分提升内容 以及JSP的内容纲要

    以下全部课件均来自于同济大学刘岩老师的<EnterpriseJavaProgramming> 因为授课语言问题,如果翻译有不正确之处欢迎指正 Section 1. 关于Servlet的部分 ...

  9. <tx:advice/> 有关的设置

    将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传播设置是 REQUIRED 隔离级别是 DEFAULT 事 ...

  10. python学习笔记:第五天( 字典)

    Python3 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格 ...