Cod sursa(job #2886600)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 7 aprilie 2022 22:10:29
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.92 kb
#include <bits/stdc++.h>
using namespace std;

class Edge{
public:
    int from;
    int to;
    int capacity;
    int flow;
    Edge* reverseEdge;

    Edge(int from, int to, int capacity, int flow = 0, Edge* reverseEdge = NULL):
         from(from), to(to), capacity(capacity), flow(flow), reverseEdge(reverseEdge){}

    int getRemainingCapacity() const{
        return capacity - flow;
    }
};

class Graph{
public:
    const int N;
    vector<vector<Edge*>> edges;

public:
    Graph(const int& N): N(N){
        edges.resize(N);
    }

    void addEdge(int from, int to, int capacity){
        Edge* e1 = new Edge(from, to, capacity);
        Edge* e2 = new Edge(to, from, 0);
        e1->reverseEdge = e2;
        e2->reverseEdge = e1;
        edges[from].push_back(e1);
        edges[to].push_back(e2);
    }
};

class EdmondsKarp{
private:
    const Graph& G;
    const int N;
    const int SRC;
    const int DEST;
    const int INF = 1e8;
    vector<int> visIdOf;
    vector<Edge*> prev;
    int visId;

    int augmentingPath(){
        queue<int> q;
        q.push(SRC);
        visIdOf[SRC] = visId;
        while(!q.empty() && visIdOf[DEST] != visId){
            int node = q.front();
            q.pop();

            for(Edge* e: G.edges[node]){
                int nextNode = e->to;
                if(visIdOf[nextNode] != visId && e->getRemainingCapacity() > 0){
                    visIdOf[nextNode] = visId;
                    prev[nextNode] = e;
                    q.push(nextNode);
                }
            }
        }

        int delta = 0;
        if(visIdOf[DEST] == visId){
            delta = INF;
            for(int node = DEST; node != SRC; node = prev[node]->from){
                delta = min(delta, prev[node]->getRemainingCapacity());
            }
            for(int node = DEST; node != SRC; node = prev[node]->from){
                prev[node]->flow += delta;
                prev[node]->reverseEdge->flow -= delta;
            }
        }

        return delta;
    }

public:
    EdmondsKarp(const Graph& G, const int& SRC, const int& DEST):
                  G(G), N(G.N), SRC(SRC), DEST(DEST){
    }

    int computeMaxFlow(){
        visIdOf.assign(N, -1);
        prev.assign(N, NULL);
        visId = 0;
        
        int maxFlow = 0;
        bool containsAugmentingPath = true;
        while(containsAugmentingPath){
            visId += 1;
            int delta = augmentingPath();
            if(delta > 0){
                maxFlow += delta;
            }else{
                containsAugmentingPath = false;
            }
        }
        
        return maxFlow;
    }
};

int main(){
    ifstream cin("maxflow.in");
    ofstream cout("maxflow.out");

    int N, M;
    cin >> N >> M;

    Graph G(N);
    int x, y, c;
    for(int i = 0; i < M; ++i){
        cin >> x >> y >> c;
        G.addEdge(x - 1, y - 1, c);
    }

    cout << EdmondsKarp(G, 0, N - 1).computeMaxFlow();

    cin.close();
    cout.close();
    return 0;
}