Алгоритм Форда-Фалкерсона: различия между версиями

Материал из Олимпиадное программирование в УлГТУ
Перейти к навигации Перейти к поиску
Нет описания правки
 
Строка 7: Строка 7:
   
   
         int other(int v) const {
         int other(int v) const {
             return v == a ? b : a;
             return v == b ? a : b;
         }
         }
   
   
Строка 21: Строка 21:
     vector<Edge> edges;
     vector<Edge> edges;
     vector<vector<int>> graph;
     vector<vector<int>> graph;
     vector<bool> visited;
     vector<int> visited, edgeTo;
    vector<int> edgeTo;
   
   
     void dfs(int v) {
     void dfs(int v) {
         visited[v] = 1;
         visited[v] = 1;
         for (int e : graph[v]) {
         for (int edgeIndex : graph[v]) {
             int to = edges[e].other(v);
             int to = edges[edgeIndex].other(v);
             if (!visited[to] && edges[e].capacityTo(to)) {
             if (!visited[to] && edges[edgeIndex].capacityTo(to)) {
                 edgeTo[to] = e;
                 edgeTo[to] = edgeIndex;
                 dfs(to);
                 dfs(to);
             }
             }
Строка 36: Строка 35:
   
   
     bool hasPath(int start, int finish) {
     bool hasPath(int start, int finish) {
         visited.assign(visited.size(), 0);
         visited.assign(graph.size(), 0);
        edgeTo.assign(graph.size(), -1);
         dfs(start);
         dfs(start);
         return visited[finish];
         return visited[finish];
     }
     }
   
   
     int bottleneckCapacity(int start, int finish) {
     int getMinCapacity(int start, int finish) {
         int bCapacity = 1e9;
         int minCapacity = 1e9;
         for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
         for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
             bCapacity = min(bCapacity, edges[edgeTo[v]].capacityTo(v));
             minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v));
         return bCapacity;
         return minCapacity;
     }
     }
   
   
Строка 54: Строка 54:
   
   
  public:
  public:
     Graph(int vertexCount) :
     Graph(int vertexCount) : graph(vertexCount) {}
        graph(vertexCount), visited(vertexCount), edgeTo(vertexCount) {}
       
     void addEdge(int from, int to, int capacity) {
     void addEdge(int from, int to, int capacity) {
         edges.push_back(Edge(from, to, capacity));
         edges.push_back(Edge(from, to, capacity));
Строка 66: Строка 65:
         long long flow = 0;
         long long flow = 0;
         while (hasPath(start, finish)) {
         while (hasPath(start, finish)) {
             int deltaFlow = bottleneckCapacity(start, finish);
             int deltaFlow = getMinCapacity(start, finish);
             addFlow(start, finish, deltaFlow);
             addFlow(start, finish, deltaFlow);
             flow += deltaFlow;
             flow += deltaFlow;

Текущая версия от 19:45, 22 октября 2025

class Graph {
    struct Edge {
        int a, b, capacity, flow = 0;

        Edge(int a, int b, int capacity) :
            a(a), b(b), capacity(capacity) {}

        int other(int v) const {
            return v == b ? a : b;
        }

        int capacityTo(int v) const {
            return v == b ? capacity - flow : flow;
        }

        void addFlowTo(int v, int deltaFlow) {
            flow += (v == b ? deltaFlow : -deltaFlow);
        }
    };

    vector<Edge> edges;
    vector<vector<int>> graph;
    vector<int> visited, edgeTo;

    void dfs(int v) {
        visited[v] = 1;
        for (int edgeIndex : graph[v]) {
            int to = edges[edgeIndex].other(v);
            if (!visited[to] && edges[edgeIndex].capacityTo(to)) {
                edgeTo[to] = edgeIndex;
                dfs(to);
            }
        }
    }

    bool hasPath(int start, int finish) {
        visited.assign(graph.size(), 0);
        edgeTo.assign(graph.size(), -1);
        dfs(start);
        return visited[finish];
    }

    int getMinCapacity(int start, int finish) {
        int minCapacity = 1e9;
        for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
            minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v));
        return minCapacity;
    }

    void addFlow(int start, int finish, int deltaFlow) {
        for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
            edges[edgeTo[v]].addFlowTo(v, deltaFlow);
    }

public:
    Graph(int vertexCount) : graph(vertexCount) {}

    void addEdge(int from, int to, int capacity) {
        edges.push_back(Edge(from, to, capacity));
        graph[from].push_back(edges.size() - 1);
        graph[to].push_back(edges.size() - 1);
    }

    long long maxFlow(int start, int finish) {
        long long flow = 0;
        while (hasPath(start, finish)) {
            int deltaFlow = getMinCapacity(start, finish);
            addFlow(start, finish, deltaFlow);
            flow += deltaFlow;
        }
        return flow;
    }
};

Ссылки

Теория:

Демонстрация:

Код:

Задачи: