1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; const int S = 0; const int T = 7; int main() { vector<vector<int>> adjMat = { {0, 8, 7, 4, 0, 0, 0, 0}, {0, 0, 2, 0, 3, 9, 0, 0}, {0, 0, 0, 5, 0, 6, 0, 0}, {0, 0, 0, 0, 0, 7, 2, 0}, {0, 0, 0, 0, 0, 0, 0, 9}, {0, 0, 0, 0, 3, 0, 4, 5}, {0, 0, 0, 0, 0, 0, 0, 8}, {0, 0, 0, 0, 0, 0, 0, 0}}; bool flag = true; while (flag) { cout << "-----------------------" << endl; for (int i = S; i <= T; i++) { for (int j = S; j <= T; j++) cout << adjMat[i][j] << " "; cout << endl; } cout << "-----------------------" << endl; queue<int> q; vector<int> prev(adjMat.size(), -1); vector<int> flow(adjMat.size(), 0); vector<int> dis(adjMat.size(), inf); q.push(S); dis[S] = 0; flag = false; while (q.size()) { auto v = q.front(); q.pop(); if (prev[v] != -1) flow[v] = min(flow[prev[v]], adjMat[prev[v]][v]); else flow[v] = inf; if (v == T) { int min_cap = flow[v]; int p = v; cout << v << " "; while (prev[p] != -1) { cout << prev[p] << " "; adjMat[prev[p]][p] -= min_cap; adjMat[p][prev[p]] += min_cap; p = prev[p]; } cout << "- min_cap: " << min_cap << endl; flag = true; break; } else { for (int i = S; i <= T; i++) { if (dis[i] > 1 + dis[v] && !flow[i] && adjMat[v][i] > 0) { dis[i] = 1 + dis[v]; prev[i] = v; q.push(i); } } } } } int f_max = 0; for (int i = S; i <= T; i++) { f_max += adjMat[T][i]; } cout << f_max << endl; return 0; }
|