1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5.  
  6. using namespace std;
  7. const int INF=;
  8. const int maxn=,maxm=;
  9. int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn];
  10.  
  11. void addedge(int a,int b,int c)
  12. {
  13. nxt[++cnt]=fir[a];
  14. to[cnt]=b;
  15. cap[cnt]=c;
  16. fir[a]=cnt;
  17. }
  18.  
  19. bool BFS(int S,int T)
  20. {
  21. memset(dis,,sizeof(dis));
  22. dis[T]=;
  23. queue<int>q;q.push(T);
  24. while(!q.empty())
  25. {
  26. int node=q.front();q.pop();
  27. for(int i=fir[node];i;i=nxt[i])
  28. {
  29. if(dis[to[i]])continue;
  30. dis[to[i]]=dis[node]+;
  31. q.push(to[i]);
  32. }
  33. }
  34. return dis[S];
  35. }
  36. int fron[maxn];
  37. int ISAP(int S,int T)
  38. {
  39. if(!BFS(S,T))
  40. return ;
  41. for(int i=;i<=T;i++)++gap[dis[i]];
  42. int p=S,ret=;
  43. memcpy(fron,fir,sizeof(fir));
  44. while(dis[S]<=T)
  45. {
  46. if(p==T){
  47. int f=INF;
  48. while(p!=S){
  49. f=min(f,cap[path[p]]);
  50. p=to[path[p]^];
  51. }
  52. p=T;ret+=f;
  53. while(p!=S){
  54. cap[path[p]]-=f;
  55. cap[path[p]^]+=f;
  56. p=to[path[p]^];
  57. }
  58. }
  59. int &ii=fron[p];
  60. for(;ii;ii=nxt[ii]){
  61. if(!cap[ii]||dis[to[ii]]+!=dis[p])
  62. continue;
  63. else
  64. break;
  65. }
  66.  
  67. if(ii){
  68. p=to[ii];
  69. path[p]=ii;
  70. }
  71.  
  72. else{
  73. if(--gap[dis[p]]==)break;
  74. int minn=T+;
  75. for(int i=fir[p];i;i=nxt[i])
  76. if(cap[i])
  77. minn=min(minn,dis[to[i]]);
  78. gap[dis[p]=minn+]++;
  79. fron[p]=fir[p];
  80. if(p!=S)
  81. p=to[path[p]^];
  82. }
  83. }
  84. return ret;
  85. }
  86.  
  87. void Init()
  88. {
  89. memset(fir,,sizeof(fir));
  90. memset(gap,,sizeof(gap));
  91. cnt=;
  92. }
  93. int main()
  94. {
  95. int n,m;
  96. while(~scanf("%d%d",&m,&n))
  97. {
  98. Init();
  99. for(int i=;i<=m;i++){
  100. int x,y,c;
  101. scanf("%d%d%d",&x,&y,&c);
  102. addedge(x,y,c);
  103. addedge(y,x,);
  104. }
  105. printf("%d\n",ISAP(,n));
  106. }
  107.  
  108. return ;
  109. }

  后来又打了一遍,代码风格都变了。(这个没有多组数据)

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5. using namespace std;
  6. const int INF=;
  7. const int maxn=;
  8. const int maxm=;
  9. int cnt=,fir[maxn],to[maxm],nxt[maxm],cap[maxm];
  10. void addedge(int a,int b,int c){
  11. nxt[++cnt]=fir[a];
  12. fir[a]=cnt;
  13. cap[cnt]=c;
  14. to[cnt]=b;
  15. }
  16.  
  17. queue<int>q;
  18. int dis[maxn];
  19. bool BFS(int s,int t){
  20. dis[t]=;q.push(t);
  21. while(!q.empty()){
  22. int x=q.front();q.pop();
  23. for(int i=fir[x];i;i=nxt[i])
  24. if(!dis[to[i]]){
  25. dis[to[i]]=dis[x]+;
  26. q.push(to[i]);
  27. }
  28. }
  29. return dis[s];
  30. }
  31.  
  32. int fron[maxn];
  33. int gap[maxn],path[maxn];
  34. int ISAP(int s,int t){
  35. if(!BFS(s,t))return ;
  36. for(int i=s;i<=t;i++)++gap[dis[i]];
  37. for(int i=s;i<=t;i++)fron[i]=fir[i];
  38. int p=s,ret=,f;
  39. while(dis[s]<=t){
  40. if(p==t){
  41. f=INF;
  42. while(p!=s){
  43. f=min(f,cap[path[p]]);
  44. p=to[path[p]^];
  45. }
  46. ret+=f;p=t;
  47. while(p!=s){
  48. cap[path[p]]-=f;
  49. cap[path[p]^]+=f;
  50. p=to[path[p]^];
  51. }
  52. }
  53. int &ii=fron[p];
  54. for(;ii;ii=nxt[ii])
  55. if(cap[ii]&&dis[p]==dis[to[ii]]+)
  56. break;
  57. if(ii)
  58. path[p=to[ii]]=ii;
  59. else{
  60. if(--gap[dis[p]]==)break;
  61. int minn=t+;
  62. for(int i=fir[p];i;i=nxt[i])
  63. if(cap[i])minn=min(minn,dis[to[i]]);
  64. ++gap[dis[p]=minn+];ii=fir[p];
  65. if(p!=s)p=to[path[p]^];
  66. }
  67. }
  68. return ret;
  69. }
  70.  
  71. int main(){
  72. int n,m;
  73. scanf("%d%d",&m,&n);
  74. for(int i=,a,b,c;i<=m;i++){
  75. scanf("%d%d%d",&a,&b,&c);
  76. addedge(a,b,c);
  77. addedge(b,a,);
  78. }
  79. printf("%d\n",ISAP(,n));
  80. return ;
  81. }

  又打一遍,很好看的结构体。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5. using namespace std;
  6. const int maxn=;
  7. const int maxm=;
  8. const int INF=;
  9. int cnt,tot,fir[maxn],fron[maxn],dis[maxn];
  10. int to[maxm],nxt[maxm],gap[maxn],path[maxn];
  11. int cap[maxm];queue<int>q;
  12.  
  13. struct Max_Flow{
  14. void Init(int tot_=){
  15. tot=tot_;cnt=;
  16. memset(fir,,sizeof(fir));
  17. memset(dis,,sizeof(dis));
  18. memset(gap,,sizeof(gap));
  19. }
  20.  
  21. void add(int a,int b,int c){
  22. nxt[++cnt]=fir[a];
  23. fir[a]=cnt;
  24. cap[cnt]=c;
  25. to[cnt]=b;
  26. }
  27.  
  28. void addedge(int a,int b,int c){
  29. add(a,b,c);
  30. add(b,a,);
  31. }
  32.  
  33. bool BFS(int s,int t){
  34. dis[t]=;q.push(t);
  35. while(!q.empty()){
  36. int x=q.front();q.pop();
  37. for(int i=fir[x];i;i=nxt[i])
  38. if(!dis[to[i]]){
  39. dis[to[i]]=dis[x]+;
  40. q.push(to[i]);
  41. }
  42. }
  43. return dis[s];
  44. }
  45.  
  46. int Aug(int s,int t,int &p){
  47. int f=INF;
  48. while(p!=s){
  49. f=min(f,cap[path[p]]);
  50. p=to[path[p]^];
  51. }p=t;
  52. while(p!=s){
  53. cap[path[p]]-=f;
  54. cap[path[p]^]+=f;
  55. p=to[path[p]^];
  56. }
  57. return f;
  58. }
  59.  
  60. int ISAP(int s,int t){
  61. if(!BFS(s,t))return ;
  62. for(int i=s;i<=t;i++)fron[i]=fir[i];
  63. for(int i=s;i<=t;i++)gap[dis[i]]+=;
  64. int p=s,ret=;
  65. while(dis[s]<=tot){
  66. if(p==t)ret+=Aug(s,t,p);
  67.  
  68. for(int &i=fron[p];i;i=nxt[i])
  69. if(cap[i]&&dis[p]==dis[to[i]]+){
  70. path[p=to[i]]=i;
  71. break;
  72. }
  73.  
  74. if(!fron[p]){
  75. if(--gap[dis[p]]==)
  76. break;
  77. int Min=tot;
  78. for(int i=fir[p];i;i=nxt[i])
  79. if(cap[i])Min=min(Min,dis[to[i]]);
  80. gap[dis[p]=Min+]+=;fron[p]=fir[p];
  81. if(p!=s)p=to[path[p]^];
  82. }
  83. }
  84. return ret;
  85. }
  86. }isap;

ISAP 模板的更多相关文章

  1. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  2. 【CodeVS 1993】草地排水 isap模板题

    开始网络流的学习,更新一下isap的模板 #include<cstdio> #include<cstring> #include<algorithm> #defin ...

  3. 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题

    [题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...

  4. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  5. HDU 3572 Task Schedule(ISAP模板&amp;&amp;最大流问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  , ...

  6. Dinic 与 SAP(ISAP?) 模板

    发一个最大流模板 DinicDinicDinic //vis为int类型 //sz为总点数 namespace Dinic { inline bool bfs() { int head = 0, ta ...

  7. 最大流isap模板

    isap+bfs初始化+栈优化,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }ed ...

  8. ISAP模板

    #include<bits/stdc++.h> using namespace std; using namespace std; typedef long long ll; const ...

  9. LOJ 101 最大流(ISAP 模板)

    开long long的最大流 #include<bits/stdc++.h> using namespace std; ;//点数的最大值 ;//边数的最大值 ; struct Edge ...

随机推荐

  1. Linux系统下查看USB设备名及使用USB设备

    1.系统插入USB设备后,从控制台界面有如下提示: 从控制台信息可以看出插入的USB设备名. 从上图可以看出,插入的USB设备为sde4. 但是,如果是CRT工具远程连接过去,可以使用下面的命令来查看 ...

  2. rabbitmq pika connection closed

    You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on  ...

  3. JavaScript 使用

    HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head> 部分 ...

  4. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    错误: 解决方案: 1."设置应用程序池默认属性"/"常规"/"启用32位应用程序",设置为 true. 如下图所示:(已测试,好使) 方法 ...

  5. 利用SQLiteOpenHelper创建数据库,进行增删改查操作

    Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...

  6. php函数serialize()与unserialize()

    serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a value ser ...

  7. PL/SQL Developer远程连接Oracle数据库

    首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora]     打开[tnsna ...

  8. Ojbect-C2 3、NSArray和NSMutableArray数组的使用

    Adopted Protocols NSCoding encodeWithCoder: initWithCoder: NSCopying copyWithZone: NSMutableCopying ...

  9. JavaScript - 测试 jQuery

    测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...

  10. wpf 自定义RadioButton控件样式

    实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式-->        & ...