Cod sursa(job #2959141)

Utilizator asparagusNadu Toma asparagus Data 29 decembrie 2022 21:30:57
Problema Flux maxim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.62 kb
#include <bits/stdc++.h>
using namespace std;


bool BFS(vector<pair<int, int>> *adjacencyList, vector<vector<pair<int, int>>> &capacityAndFlow, int *parentOf, int numberOfNodes, int startNode, int sinkNode) {
    bool isVisited[numberOfNodes];
    for (int i=0; i<numberOfNodes; i++)
        isVisited[i] = false;

    queue<int> workingQueue;
    workingQueue.push(startNode);
    isVisited[startNode] = true;

    while (not workingQueue.empty()) {
        int currentNode = workingQueue.front();
        workingQueue.pop();

        for (auto adjacentNode : adjacencyList[currentNode])
            if (capacityAndFlow[currentNode][adjacentNode.first].first - capacityAndFlow[currentNode][adjacentNode.first].second > 0) {
                if (adjacentNode.first == sinkNode) {
                    parentOf[sinkNode] = currentNode;
                    return true;
                }
                else if (not isVisited[adjacentNode.first]) {
                    workingQueue.push(adjacentNode.first);
                    parentOf[adjacentNode.first] = currentNode;
                    isVisited[currentNode] = true;
                }
            }
    }

    return false;
}


int getMaxFlowEdmondsKarp(vector<pair<int, int>> *adjacencyList, int numberOfNodes, int startNode, int sinkNode) {
    vector<vector<pair<int,int>>> capacityAndFlow(numberOfNodes, vector<pair<int, int>>(numberOfNodes, pair<int, int>()));
    vector<pair<int,int>> workingAdjacencyList[numberOfNodes];

    for (int node = 0; node<numberOfNodes; node++)
        for (auto adjacentNode : adjacencyList[node]) {
            workingAdjacencyList[node].push_back({adjacentNode.first, adjacentNode.second});
            workingAdjacencyList[adjacentNode.first].push_back({node, adjacentNode.second});

            capacityAndFlow[node][adjacentNode.first].first = adjacentNode.second;
            capacityAndFlow[node][adjacentNode.first].second = 0;

            capacityAndFlow[adjacentNode.first][node].first = adjacentNode.second;
            capacityAndFlow[adjacentNode.first][node].second = adjacentNode.second;
        }

    int parentOf[numberOfNodes], maxFlow = 0;
    while (BFS(workingAdjacencyList, capacityAndFlow, parentOf, numberOfNodes, startNode, sinkNode)) {
        int currentFlowUnits = INT_MAX;

        for (int childNode = sinkNode; childNode!=startNode; childNode = parentOf[childNode]) {
            int parentNode = parentOf[childNode];

            if (capacityAndFlow[parentNode][childNode].first - capacityAndFlow[parentNode][childNode].second < currentFlowUnits)
                currentFlowUnits = capacityAndFlow[parentNode][childNode].first - capacityAndFlow[parentNode][childNode].second;
        }

        for (int childNode = sinkNode; childNode != startNode; childNode = parentOf[childNode]) {
            int parentNode = parentOf[childNode];

            capacityAndFlow[parentNode][childNode].second +=currentFlowUnits;
            capacityAndFlow[childNode][parentNode].second -=currentFlowUnits;
        }

        maxFlow +=currentFlowUnits;
    }

    return maxFlow;
}


int main() {
    ifstream input("maxflow.in");

    int numberOfNodes, numberOfEdges;
    input>>numberOfNodes>>numberOfEdges;

    vector<pair<int, int>> adjacencyList[numberOfNodes];
    int parentNode, childNode, capacity;
    for (int i = 0; i<numberOfEdges; i++){
        input>>parentNode>>childNode>>capacity;
        parentNode--; childNode--;
        adjacencyList[parentNode].push_back({childNode, capacity});
    }

    input.close();


    ofstream output("maxflow.out");
    output<<getMaxFlowEdmondsKarp(adjacencyList, numberOfNodes, 0, numberOfNodes - 1);
    output.close();

    return 0;
}