Cod sursa(job #2246454)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 27 septembrie 2018 09:27:25
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define DIM 1002

using namespace std;

ifstream in ("maxflow.in");
ofstream out("maxflow.out");

int nodeNum, edgeNum, maxFlow;
int capacity[DIM][DIM], flow[DIM][DIM], father[DIM];

vector<int> graph[DIM];

bitset<DIM> visited;

queue<int> nodeQueue;

bool bfs(){
    nodeQueue.push(1);
    visited[1] = true;
    while(!nodeQueue.empty()){
        int currentNode = nodeQueue.front();
        for(auto nextNode : graph[currentNode]){
            if(!visited[nextNode] && capacity[currentNode][nextNode] - flow[currentNode][nextNode] >= 0){
                visited[nextNode] = 1;
                nodeQueue.push(nextNode);
                father[nextNode] = currentNode;
            }
        }
        nodeQueue.pop();
    }
}

int main()
{
    in>>nodeNum>>edgeNum;
    for(int edgeCnt = 1; edgeCnt <= edgeNum; ++ edgeCnt){
        int node1, node2;
        in>>node1>>node2>>capacity[node1][node2];
    }

    while(bfs()){
        for(auto nodeCnt : graph[nodeNum]){
            if(capacity[nodeCnt][nodeNum] - flow[nodeCnt][nodeNum] <= 0 || !visited[nodeCnt])
                continue;
            int minFlow = capacity[nodeCnt][nodeNum] - flow[nodeCnt][nodeNum];
            int currentNode = nodeCnt;
            while(currentNode != 1){
                minFlow = min(minFlow, capacity[father[currentNode]][currentNode]);
                currentNode = father[currentNode];
            }
            currentNode = nodeCnt;
            flow[currentNode][nodeNum] += minFlow;
            flow[nodeNum][currentNode] -= minFlow;
            maxFlow == minFlow;
            while(currentNode != 1){
                flow[father[currentNode]][currentNode] += minFlow;
                flow[currentNode][father[currentNode]] -= minFlow;
                currentNode = father[currentNode];
            }

        }
    }

    out<<maxFlow;


    return 0;
}