Cod sursa(job #2387637)

Utilizator EclipseTepes Alexandru Eclipse Data 24 martie 2019 22:30:31
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <vector>
#include <cstring>
#include <deque>

using namespace std;

const int dmax = 1e3 + 5;
const int INF = 1e9;

typedef pair<int, int> pi;

int n, m;
int x, y, z;
int maxFlow, flow;
int parent[dmax], fl[dmax];
bool viz[dmax];
deque<int> d;
int newV, pVerif, newFlow;

int FlowMatrix[dmax][dmax], CapacityMatrix[dmax][dmax];

bool BFS() {
    int i, j;
    for (i = 1; i <= n; i++) viz[i] = false, parent[i] = 0;
    d.clear();
    d.push_back(1);
    while (!d.empty() && !viz[n]) {
        pVerif = d.front();
        d.pop_front();
        viz[pVerif] = true;
        for (i = 1; i <= n; i++) {
            if (CapacityMatrix[pVerif][i] && !viz[i]) {
                parent[i] = pVerif;
                d.push_back(i);
            }
        }
    }
    return viz[n];
}

void FindMinFlow() {
    newFlow = INF;
    pVerif = n;
    while (pVerif != 1) {
        newFlow = min(newFlow, CapacityMatrix[parent[pVerif]][pVerif]);
        pVerif = parent[pVerif];
    }
    maxFlow += newFlow;
}

void AugmentPath() {
    pVerif = n;
    while (pVerif != 1) {
        CapacityMatrix[parent[pVerif]][pVerif] -= newFlow;
        CapacityMatrix[pVerif][parent[pVerif]] += newFlow;
        pVerif = parent[pVerif];
    }
}

void FordFulkerson() {
    while (BFS()) {
        FindMinFlow();
        AugmentPath();
    }
}

int main()
{
    int i, j;
    cin >> n >> m;
    for (i = 1; i <= m; i++) {
        cin >> x >> y >> z;
        CapacityMatrix[x][y] = z;
    }

    FordFulkerson();
    cout << maxFlow;

    return 0;
}