Cod sursa(job #2886575)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 7 aprilie 2022 21:37:35
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.46 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 FordFulkerson{
private:
    const Graph& G;
    const int N;
    const int SRC;
    const int DEST;
    vector<bool> vis;

    int augmentingPath(int node, int minDelta){
        if(vis[node]){
            return 0;
        }
        
        vis[node] = true;
        
        if(node == DEST){
            return minDelta;
        }

        for(Edge* e: G.edges[node]){
            if(e->getRemainingCapacity() > 0){
                int delta = augmentingPath(e->to, min(minDelta, e->getRemainingCapacity()));
                if(delta > 0){
                    e->flow += delta;
                    e->reverseEdge->flow -= delta;
                    return delta;
                }
            }
        }

        return 0;
    }

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

    int computeMaxFlow(){
        vis.assign(N, false);

        int maxFlow = 0;
        bool containsAugmentingPath = true;
        while(containsAugmentingPath){
            fill(vis.begin(), vis.end(), false);
            int delta = augmentingPath(SRC, INT_MAX);
            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 << FordFulkerson(G, 0, N - 1).computeMaxFlow();

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