Cod sursa(job #2591890)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 31 martie 2020 17:09:27
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1005;

bool viz[MAXN];
queue<int> coada;
int cap[MAXN][MAXN], flux[MAXN][MAXN], sursa[MAXN];
vector<int> graf[MAXN];

int main()
{
    ifstream fin("maxflow.in");
    ofstream fout("maxflow.out");
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i){
        int x, y, c;
        fin >> x >> y >> c;
        cap[x][y] = c;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    bool sink = 1;
    int maxflux = 0;
    while(sink){
        sink = 0;
        memset(viz, 0, n + 1);
        coada.push(1);
        viz[1] = 1;
        while(!coada.empty()){
            int vf = coada.front();
            coada.pop();
            for(auto x: graf[vf]){
                if(!viz[x] && cap[vf][x] != flux[vf][x]){
                    viz[x] = 1;
                    sursa[x] = vf;
                    if(x == n) sink = 1;
                    else coada.push(x);
                }
            }
        }
        int mnflux = 1e9, crt = n;
        while(crt > 1){
            mnflux = min(mnflux, cap[sursa[crt]][crt] - flux[sursa[crt]][crt]);
            crt = sursa[crt];
        }
        crt = n;
        while(crt > 1){
            flux[sursa[crt]][crt] += mnflux;
            flux[crt][sursa[crt]] -= mnflux;
            crt = sursa[crt];
        }
        maxflux += mnflux;
    }
    fout << maxflux;
    return 0;
}