Cod sursa(job #1893099)

Utilizator blackoddAxinie Razvan blackodd Data 25 februarie 2017 14:42:50
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>

using namespace std;

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

#define MaxN 1005
#define INF 0x3f3f3f3f

int n, m, x, y, z, flow, flmin;
bool viz[MaxN];
vector<int> G[MaxN];
int c[MaxN][MaxN], f[MaxN][MaxN], t[MaxN];

bool Bf() {
    queue<int> Q;
    memset(viz, 0, sizeof (viz) );
    Q.push(1);

    while(Q.size()) {
        int nod = Q.front();
        Q.pop();
        viz[nod] = true;
        for ( const auto x : G[nod] )
            if ( !viz[x] && f[nod][x] < c[nod][x] ) {
                t[x] = nod;
                Q.push(x);
        }
    }

    return viz[n];
}

int main() {
    fin >> n >> m;

    for ( int i = 1; i <= m; ++i ) {
        fin >> x >> y >> z;
        c[x][y] += z;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for ( flow = 0; Bf(); )
        for ( const auto x : G[n] ) {
            if ( f[x][n] < c[x][n] && viz[x] ) {
                t[n] = x;
                flmin = INF;

                for ( int nod = n; nod != 1; nod = t[nod] )
                    flmin = min(flmin, c[t[nod]][nod] - f[t[nod]][nod]);

                if ( flmin == 0 ) continue;

                for ( int nod = n; nod != 1; nod = t[nod] ) {
                    f[t[nod]][nod] += flmin;
                    f[nod][t[nod]] -= flmin;
                }

                flow += flmin;

            }
        }

    fout << flow;


}