Cod sursa(job #2220163)

Utilizator EclipseTepes Alexandru Eclipse Data 10 iulie 2018 19:28:23
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#define flow first
#define capacity second
#define dMAX (1 << 10)
#define INF  (1 << 10)

using namespace std;

int n, m;
int x, y, c;
int u, pVerif, newV;
int fmin, maxFlow;

int capacity[dMAX + 2][dMAX + 2], flow[dMAX + 2][dMAX + 2];

vector<int> graf[dMAX + 2];
int father[dMAX + 2];

int myDeque[dMAX + 2], h;
int viz[dMAX + 2];

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

bool BFS() {
    int i, j;
    memset(viz, false, sizeof(viz));
    viz[1] = true;
    myDeque[1] = h = 1;
    for (i = 1; i <= h; i++) {
        pVerif = myDeque[i];
        if (pVerif == n) continue;
        for (u = 0; u < graf[pVerif].size(); u++) {
            newV = graf[pVerif][u];
            if (capacity[pVerif][newV] == flow[pVerif][newV] || viz[newV]) continue;
            viz[newV] = true;
            myDeque[++h] = newV;
            father[newV] = pVerif;
        }
    }
    return viz[n];
}

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

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

    }

    fout << maxFlow;

    return 0;
}