Cod sursa(job #2682833)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 9 decembrie 2020 18:39:21
Problema Flux maxim Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>
#define oo (1 << 30)
#define MAX 1005
using namespace std;

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

int n, m, x ,y , cost, flow;
int cap[MAX][MAX], flux[MAX][MAX], viz[MAX], t[MAX];

vector < int > g[MAX];
deque < int > d;

int BFS(){
    int nod;
    memset(viz, 0, sizeof(viz));
    viz[1] = 1;
    d.push_back(1);
    while(!d.empty()){
        nod = d.back();
        d.pop_back();
        for(int i = 0; i < g[nod].size(); i++){
            int vec = g[nod][i];
            if(viz[vec] || cap[nod][vec] == flux[nod][vec])
                continue;
            viz[vec] = 1;
            t[vec] = nod;
            d.push_front(vec);
        }
    }

    return viz[n];
}

int main(){
    in>>n>>m;
    while(m--){
        in>>x>>y>>cost;
        cap[x][y] += cost;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for( flow = 0; BFS();)
    for(int i = 0; i < g[n].size(); i++){
        int nod = g[n][i];
        if(flux[nod][n] == cap[nod][n] || !viz[nod])
            continue;
        t[n] = nod;
        int fluxmin = oo;
        for(nod = n; nod != 1; nod = t[nod])
            fluxmin = min(fluxmin, cap[t[nod]][nod] - flux[t[nod]][nod]);

        if(!fluxmin)
            continue;
        for(nod = n; nod != 1; nod = t[nod]){
            flux[t[nod]][nod] += fluxmin;
            flux[nod][t[nod]] -= fluxmin;
        }

        flow += fluxmin;
    }

    out<<flow;

}