Cod sursa(job #2238449)

Utilizator EclipseTepes Alexandru Eclipse Data 5 septembrie 2018 18:53:03
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <deque>
#define dMAX (1 << 10)
#define INF (1 << 19)

using namespace std;

int n, m;
int x, y, c;
int currentFlow, maxFlow;

int C[dMAX][dMAX], F[dMAX][dMAX];

vector<int> graf[dMAX];
deque<int> myDeque;
int pVerif, newV, father[dMAX];
bool viz[dMAX];

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

bool BFS() {
    int i, j;
    memset(viz, false, sizeof(viz));
    viz[1] = true;
    myDeque.clear();
    myDeque.push_back(1);
    while (!myDeque.empty()) {
        pVerif = myDeque.front();
        myDeque.pop_front();
        if (pVerif == n) continue;
        for (i = 0; i < graf[pVerif].size(); i++) {
            newV = graf[pVerif][i];
            if (!viz[newV]) {
                if (F[pVerif][newV] != C[pVerif][newV]) {
                    father[newV] = pVerif;
                    viz[newV] = true;
                    myDeque.push_back(newV);
                    //if (newV == n) return true;
                }
            }
        }
    }
    return viz[n];
}

int main() {
    int i, j;
    fin >> n >> m;
    for (i = 1; i <= m; i++) {
        fin >> x >> y >> c;
        C[x][y] = c;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }

    while (BFS()) {
        for (i = 0; i < graf[n].size(); i++) {
            newV = graf[n][i];
            currentFlow = INF;
            if (!viz[newV] || (C[newV][n] == F[newV][n])) continue;
            father[n] = newV;
            for (j = n; j != 1; j = father[j]) currentFlow = min(currentFlow, C[father[j]][j] - F[father[j]][j]);
            for (j = n; j != 1; j = father[j]) {
                F[father[j]][j] += currentFlow;
                F[j][father[j]] -= currentFlow;
            }
            maxFlow += currentFlow;
        }
    }

    fout << maxFlow;

    return 0;
}