Cod sursa(job #2886090)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 6 aprilie 2022 22:48:44
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.35 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;
    }

    int computeMaxCapacity() const{
        int maxCapacity = capacity[0][0];
        for(int i = 0; i < N; ++i){
            for(int j = 0; j < N; ++j){
                maxCapacity = max(maxCapacity, capacity[i][j]);
            }
        }
        return maxCapacity;
    }
};

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;

    int augmentingPath(const int& CAPACITY_THRESHOLD){
        fill(vis.begin(), vis.end(), false);
        fill(prev.begin(), prev.end(), INF);

        queue<int> q;
        q.push(SRC);
        vis[SRC] = true;
        while(!q.empty() && !vis[DEST]){
            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 >= CAPACITY_THRESHOLD){
                    vis[nextNode] = true;
                    prev[nextNode] = node;
                    q.push(nextNode);
                }
            }
        }

        int delta = 0;
        if(vis[DEST]){
            delta = INF;
            for(int node = DEST; node != SRC; node = prev[node]){
                int remainingCapacity = G.capacity[prev[node]][node] - f[prev[node]][node];
                delta = min(delta, remainingCapacity);
            }
            for(int node = DEST; node != SRC; node = prev[node]){
                f[prev[node]][node] += delta;
                f[node][prev[node]] -= 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(){
        f.assign(N, vector<int>(N, 0));
        vis.resize(N);
        prev.resize(N);
        
        int maxFlow = 0;
        int maxCapacity = G.computeMaxCapacity();
        int capacityThreshold = 1;
        while(capacityThreshold < maxCapacity){
            capacityThreshold *= 2;
        }

        while(capacityThreshold > 0){
            bool containsAugmentingPath = true;
            while(containsAugmentingPath){
                fill(vis.begin(), vis.end(), false);
                int delta = augmentingPath(capacityThreshold);
                if(delta > 0){
                    maxFlow += delta;
                }else{
                    containsAugmentingPath = false;
                }
            }
            capacityThreshold /= 2;
        }
        
        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;
}