Cod sursa(job #1864477)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 31 ianuarie 2017 19:53:04
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <iostream>
#include <deque>

using namespace std;

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

const int f_mare = 2e9;
int n, m, x, y, z, c[1005][1005], f[1005][1005], sol, fmin, tt[1005];
bool viz[1005];
deque <int> q;
vector <int> ls[1005];

bool umplut(int x, int y) {
    return (c[x][y]-f[x][y] == 0);
}

bool bf() {
    int i, l, x, y;
    memset(viz, 0, sizeof(viz));
    q.clear();
    q.push_back(1);
    while (q.empty() == 0) {
        x = q.front();
        q.pop_back();
        l = ls[x].size();
        viz[1] = 1;
        for (i = 0; i < l; i++) {
            y = ls[x][i];
            if (viz[y]|| umplut(x,y))
                continue;
            viz[y] = 1;
            tt[y] = x;
            q.push_back(y);
            viz[y] = 1;
            if (y == n) return 1;
        }
    }
    //cout<<1;
    return 0;
}

int main() {
    in >> n >> m;
    while (m--) {
        in >> x >> y >> z;
        ls[x].push_back(y);
        ls[y].push_back(x);
        c[x][y] += z;
    }
    while (bf()) {
        fmin = f_mare;
        y = n;
        while (y != 1)
            fmin = min(fmin, c[tt[y]][y]-f[tt[y]][y]), y = tt[y];
        y = n;
        while (y != 1) {
            f[tt[y]][y] += fmin;
            f[y][tt[y]] -= fmin;
            y = tt[y];
        }
        sol += fmin;
    }
    out << sol;
    return 0;
}