Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ffDFS(int source, int sink, int flow, std::vector<Edge>& edges, std::vector<int>& head, std::vector<bool>& used) {
- if (source == sink) {
- return flow;
- }
- used[source] = true;
- for (int i = head[source]; i != -1; i = edges[i].next) {
- if (!used[edges[i].to] && (edges[i].flow < edges[i].capacity)) {
- int pushed = ffDFS(edges[i].to, sink, std::min(flow, edges[i].capacity - edges[i].flow), edges, head, used);
- if (pushed) {
- edges[i].flow += pushed;
- edges[i ^ 1].flow -= pushed;
- return pushed;
- }
- }
- }
- return 0;
- }
- Graph flowFordFulkerson(int sourse, int sink) {
- int n;
- int r;
- int w;
- graph->getProperties(n, r, w);
- Graph result(type, n, r, w);
- std::vector<Edge> edges;
- std::vector<int> head(n + 1, -1);
- std::vector<bool> used(n + 1, false);
- transformToListOfEdges();
- for (auto& edge : graph->getListOfEdges()) {
- int a;
- int b;
- int w;
- std::tie(a, b, w) = edge;
- edges.push_back(Edge{a, b, head[a], 0, w});
- edges.push_back(Edge{b, a, head[b], 0, 0});
- head[a] = edges.size() - 2;
- head[b] = edges.size() - 1;
- }
- for (;;) {
- if (ffDFS(sourse, sink, std::numeric_limits<int>::max(), edges, head, used) == 0) {
- break;
- }
- used.assign(n + 1, false);
- }
- for (int i = 0; i < int(edges.size()); i += 2) {
- result.addEdge(edges[i].from, edges[i].to, edges[i].flow);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement