Cod sursa(job #2655134)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 3 octombrie 2020 12:51:06
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1005;
int n, m, c[nmax][nmax], f[nmax][nmax], t[nmax];
bool viz[nmax];
vector <int> G[nmax];

bool dfs(){
    memset(viz, 0, sizeof viz);
    queue <int> coada;
    coada.push(1);
    viz[1] = true;
    while (!coada.empty()){
        int nod = coada.front();
        coada.pop();
        for (auto it : G[nod]){
            if (viz[it] == true || f[nod][it] == c[nod][it]) continue;
            coada.push(it);
            viz[it] = true;
            t[it] = nod;
            if (it == n){
                return true;
            }
        }
    }
    return false;
}

int main(){
    fin >> n >> m;
    for (int i = 1; i <= m; ++i){
        int x, y, z;
        fin >> x >> y >> z;
        G[x].push_back(y);
        G[y].push_back(x);
        c[x][y] += z;
    }
    int flow, fmin;
    for (flow = 0; dfs(); flow += fmin){
        fmin = 1e9;
        for (int nod = n; nod != 1; nod = t[nod]){
            fmin = min(fmin, c[t[nod]][nod] - f[t[nod]][nod]);
        }
        for (int nod = n; nod != 1; nod = t[nod]){
            f[t[nod]][nod] += fmin;
            f[nod][t[nod]] -= fmin;
        }
    }
    fout << flow << "\n";
    fin.close();
    fout.close();
    return 0;
}