Cod sursa(job #2162791)

Utilizator CammieCamelia Lazar Cammie Data 12 martie 2018 14:00:12
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <fstream>
#include <vector>
#include <cstring>

#define MAXN 1005

using namespace std;

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

int N, M, sol, dad[MAXN], viz[MAXN], a[MAXN][MAXN], mini[MAXN], ok;

struct str{
    int node, c;
};

vector <str> graph[MAXN];

inline void Read() {
    int x, y, z;

    fin >> N >> M;

    for (int i = 1; i <= M; i++) {
        fin >> x >> y >> z;

        graph[x].push_back({y, z});
    }
}

inline void Refa(int node, int minim) {
    int aux;
    while (dad[node]) {
        aux = dad[node];
        a[aux][node] += minim;

        node = dad[node];
    }

}

inline void DFS(int node) {
    if (node == N) {
        Refa(node, mini[N]); sol += mini[N];
        ok = 1;
        return;
    }

    if (ok == 1)
        return;

    for (auto x : graph[node]) {
        x.c -= a[node][x.node];

        if (x.c > 0 && viz[x.node] == 0) {
            if (x.c < mini[node]) {
                mini[x.node] = x.c;
            }
            else
                mini[x.node] = mini[node];
            viz[x.node] = 1; dad[x.node] = node;

            DFS(x.node);

            if (ok == 1)
                return;
        }
    }
}

int main () {
    Read();
    ok = 1; mini[1] = 0x3f3f3f3f;
    while (ok == 1) {
        memset(viz, 0, sizeof(viz));
        ok = 0;
        DFS(1);
    }

    fout << sol;

    fin.close(); fout.close(); return 0;
}