Cod sursa(job #2886349)

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

class Graph{
public:
    const int N;
    vector<vector<int>> neighbors;
    vector<vector<int>> capacity;

public:
    Graph(const int& N): N(N){
        neighbors.resize(N);
        capacity.assign(N, vector<int>(N));
    }

    void addEdge(int x, int y, int c){
        neighbors[x].push_back(y);
        neighbors[y].push_back(x);
        capacity[x][y] = c;
    }
};

class EdmondsKarp{
private:
    const Graph& G;
    const int N;
    const int SRC;
    const int DEST;
    const int INF = 1e8;
    vector<vector<int>> f;
    vector<bool> vis;
    vector<int> prev;

    void bfs(){
        fill(vis.begin(), vis.end(), false);
        fill(prev.begin(), prev.end(), INF);

        queue<int> q;
        q.push(SRC);
        vis[SRC] = true;
        while(!q.empty()){
            int node = q.front();
            q.pop();

            for(int nextNode: G.neighbors[node]){
                int remainingCapacity = G.capacity[node][nextNode] - f[node][nextNode];
                if(!vis[nextNode] && remainingCapacity > 0){
                    vis[nextNode] = true;
                    prev[nextNode] = node;
                    q.push(nextNode);
                }
            }
        }
    }

    int augmentingPaths(){
        bfs();

        int deltaSum = 0;
        if(vis[DEST]){
            for(int prevDest: G.neighbors[DEST]){
                int delta = G.capacity[prevDest][DEST] - f[prevDest][DEST];
                for(int node = prevDest; node != SRC && delta > 0; node = prev[node]){
                    int remainingCapacity = G.capacity[prev[node]][node] - f[prev[node]][node];
                    delta = min(delta, remainingCapacity);
                }
                f[prevDest][DEST] += delta;
                f[DEST][prevDest] -= delta;
                for(int node = prevDest; node != SRC && delta > 0; node = prev[node]){
                    f[prev[node]][node] += delta;
                    f[node][prev[node]] -= delta;
                }
                deltaSum += delta;
            }
        }

        return deltaSum;
    }

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

    int computeMaxFlow(){
        f.assign(N, vector<int>(N, 0));
        vis.resize(N);
        prev.resize(N);
        
        int maxFlow = 0;
        bool containsAugmentingPath = true;
        while(containsAugmentingPath){
            int deltaSum = augmentingPaths();
            if(deltaSum > 0){
                maxFlow += deltaSum;
            }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;
}