http://acm.hdu.edu.cn/showproblem.php?pid=3549
题意:
给出N个点M条边,求1到N的最大流量。
坑爹:
模板
解法:
在白书上看到的ISAP算法就敲了个模板出来A了这道题,就是想测试一下EK和ISAP的时间
差多少,EK是921MS,ISAP是109MS。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<vector> 4 #include<queue> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 const int INF = 0x3fffffff; 9 10 struct Edge { 11 int from; 12 int to; 13 int cap; 14 int flow; 15 }edge[2*maxn]; 16 vector<int> G[maxn]; 17 int head[maxn]; 18 int cur[maxn]; 19 bool used[maxn]; 20 int deep[maxn]; 21 int front[maxn]; 22 int gap[maxn]; 23 int cnt; 24 int N; 25 int M; 26 27 void addEdge(int from, int to, int cap) 28 { 29 edge[cnt].from = from; 30 edge[cnt].to = to; 31 edge[cnt].cap = cap; 32 edge[cnt].flow = 0; 33 cnt ++; 34 35 edge[cnt].from = to; 36 edge[cnt].to = from; 37 edge[cnt].cap = 0; 38 edge[cnt].flow = 0; 39 cnt ++; 40 41 G[from].push_back(cnt-2); 42 G[to].push_back(cnt-1); 43 } 44 45 bool BFS(int s, int t) 46 { 47 memset(used,0,sizeof(used)); 48 queue<int> Q; 49 Q.push(t); 50 deep[t] = 0; 51 used[t] = true; 52 while(!Q.empty()) 53 { 54 int top = Q.front(); 55 Q.pop(); 56 int i; 57 for(i=0; i<G[top].size(); i++) 58 { 59 Edge e = edge[G[top][i]]; 60 if(!used[e.to]) 61 { 62 used[e.to] = true; 63 deep[e.to] = deep[top] + 1; 64 Q.push(e.to); 65 } 66 } 67 } 68 return used[s]; 69 } 70 71 int Augment(int s,int t) 72 { 73 int begin = t; 74 int minFlow = INF; 75 while(begin != s) 76 { 77 Edge& e = edge[front[begin]]; 78 minFlow = min(minFlow, e.cap-e.flow); 79 begin = edge[front[begin]].from; 80 } 81 begin = t; 82 while(begin != s) 83 { 84 edge[front[begin]].flow += minFlow; 85 edge[front[begin]^1].flow -= minFlow; 86 begin = edge[front[begin]].from; 87 } 88 return minFlow; 89 } 90 91 int Maxflow(int s,int t) 92 { 93 int flow = 0; 94 BFS(s,t); 95 memset(gap,0,sizeof(gap)); 96 memset(cur,0,sizeof(cur)); 97 int i; 98 for(i=0; i<=N; i++) 99 { 100 gap[deep[i]] ++; 101 } 102 int begin = s; 103 while(deep[s] < N) 104 { 105 if(begin == t) 106 { 107 flow += Augment(s,t); 108 begin = s; 109 } 110 bool ok = false; 111 int i; 112 for(i=cur[begin]; i<G[begin].size(); i++) 113 { 114 Edge& e = edge[G[begin][i]]; 115 if(e.cap > e.flow && deep[begin] == deep[e.to] + 1) 116 { 117 ok = true; 118 front[e.to] = G[begin][i]; 119 cur[begin] = i; 120 begin = e.to; 121 break; 122 } 123 } 124 if(!ok) 125 { 126 int m = N - 1; 127 int i; 128 for(i=0; i<G[begin].size(); i++) 129 { 130 Edge& e = edge[G[begin][i]]; 131 if(e.cap > e.flow) 132 { 133 m = min(m, deep[e.to]); 134 } 135 } 136 if(--gap[deep[begin]] == 0) 137 { 138 break; 139 } 140 gap[deep[begin] = m+1] ++; 141 cur[begin] = 0; 142 if(begin != s) 143 { 144 begin = edge[front[begin]].from; 145 } 146 } 147 } 148 return flow; 149 } 150 151 int main() 152 { 153 int T; 154 scanf("%d",&T); 155 int Case; 156 for(Case=1; Case<=T; Case++) 157 { 158 scanf("%d%d",&N,&M); 159 int i; 160 for(i=0; i<=N; i++) 161 { 162 G[i].clear(); 163 } 164 cnt = 0; 165 memset(deep,0,sizeof(deep)); 166 for(i=0; i<M; i++) 167 { 168 int from; 169 int to; 170 int cap; 171 scanf("%d%d%d",&from,&to,&cap); 172 addEdge(from,to,cap); 173 } 174 int flow = Maxflow(1,N); 175 printf("Case %d: %d\n",Case,flow); 176 } 177 return 0; 178 }