代码当中是以边的方式存图,邻接矩阵类似。
1 int dfs(int u, int flow) 2 { 3 int tmp, out = 0; 4 for (int p = fst[u]; p; p = nxt[p]) { 5 if (w[p] <= 0 || dis[u] != dis[v[p]] + 1) 6 continue; 7 w[p] -= tmp = dfs(v[p], min(flow - out, w[p])); 8 w[p + 1] += tmp; 9 if ((out += tmp) == flow || dis[u] >= n) 10 return out; 11 } 12 if (dis[u] >= n) 13 return out; 14 if (!--cnt[dis[u]]) 15 dis[u] = n; 16 ++cnt[++dis[u]]; 17 return out; 18 } 19 20 int main() 21 { 22 /* input */ 23 24 while (dis[1] < n) 25 ans += dfs(1, INF_INT); 26 printf("%d\n", ans); 27 return 0; 28 }
时间: 2024-10-27 19:10:46