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

Материал из Олимпиадное программирование в УлГТУ
Перейти к навигации Перейти к поиску
Нет описания правки
 
Строка 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 bfs(int start) {
     bool hasPath(int start, int finish) {
        visited.assign(graph.size(), 0);
        edgeTo.assign(graph.size(), -1);
         queue<int> q;
         queue<int> q;
         visited[start] = 1;
         visited[start] = 1;
         q.push(start);
         q.push(start);
         while (!q.empty()) {
         while (!q.empty()) {
             int v = q.front();
             int v = q.front();
             q.pop();
             q.pop();
             for (int e : graph[v]) {
                 int to = edges[e].other(v);
             for (int edgeIndex : graph[v]) {
                 if (!visited[to] && edges[e].capacityTo(to)) {
                 int to = edges[edgeIndex].other(v);
                    edgeTo[to] = e;
                 if (!visited[to] && edges[edgeIndex].capacityTo(to)) {
                     visited[to] = 1;
                     visited[to] = 1;
                    edgeTo[to] = edgeIndex;
                     q.push(to);
                     q.push(to);
                 }
                 }
             }
             }
         }
         }
    }
   
   
    bool hasPath(int start, int finish) {
        visited.assign(visited.size(), 0);
        bfs(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;
     }
     }
   
   
Строка 61: Строка 61:
   
   
  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) {
Строка 73: Строка 72:
         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:47, 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;

    bool hasPath(int start, int finish) {
        visited.assign(graph.size(), 0);
        edgeTo.assign(graph.size(), -1);
        queue<int> q;

        visited[start] = 1;
        q.push(start);

        while (!q.empty()) {
            int v = q.front();
            q.pop();

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

        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;
    }
};

Ссылки

Теория:

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

Код:

Задачи: