Cod sursa(job #1893146)

Utilizator blackoddAxinie Razvan blackodd Data 25 februarie 2017 15:01:21
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 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;
        if ( nod == n ) continue;
        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;
 
 
}