Cod sursa(job #2954361)

Utilizator T_george_TGeorge Teodorescu T_george_T Data 14 decembrie 2022 01:01:30
Problema Flux maxim Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <string.h>
using namespace std;

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

const int NMAX = 1e2;

int n,m,cap[NMAX][NMAX],seen[NMAX],f[NMAX][NMAX],father[NMAX];

vector<int>g[NMAX];

int bf(){
    queue<int>q;
    memset(seen,0,sizeof(seen));

    q.push(1);
    seen[1]=1;
    while(!q.empty()){
        int node=q.front();
        q.pop();
        for(int next:g[node]){
            if(cap[node][next]==f[node][next] || seen[next])
                continue;
            seen[node]=1;
            q.push(next);
            father[next]=node;
            if(next==n)
                return 1;
        }
    }

    return 0;
}

int main() {

    in>>n>>m;

    for(int i=1,a,b,c;i<=m;i++){
        in>>a>>b>>c;
        g[a].push_back(b);
        g[b].push_back(a);
        cap[a][b] = c;
    }
    int flow,fmin;
    for( flow=0,fmin;bf();flow += fmin){
        fmin=2e9;
        for(int node = n;node!=1;node =father[node]){
            fmin = min(fmin, cap[father[node]][node]-f[father[node]][node]);
        }
        for(int node = n; node != 1;node = father[node]){
            f[father[node]][node]+=fmin;
            f[node][father[node]]-=fmin;
        }
    }

    out<<flow;

    return 0;
}