Cod sursa(job #2238446)

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

using namespace std;

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

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

vector<int> graph[dMAX + 2];
bool viz[dMAX + 2];

deque<int> myDeque;
int pVerif, newV;
int father[dMAX + 2];
int currentFlow, maxFlow;

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();
        for (i = 0; i < graph[pVerif].size(); i++) {
            newV = graph[pVerif][i];
            if (F[pVerif][newV] != C[pVerif][newV]) {
                if (!viz[newV]) {
                     viz[newV] = true;
                     father[newV] = pVerif;
                     myDeque.push_back(newV);
                     if (newV == n) return true;
                }
            }
        }
    }
    return false;
}

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

    while (BFS()) {

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

    fout << maxFlow;

    return 0;
}