Cod sursa(job #3156217)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 10 octombrie 2023 21:04:46
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.91 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("O3,unroll-loops")
#pragma GCC target ("popcnt")

using namespace std;

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

const int MAX_N = 1000;
const int MAX_M = 5000;
int n, m;
vector<int> edge[MAX_N + 5];

int cap[MAX_N + 5][MAX_N + 5], flow[MAX_N + 5][MAX_N + 5];
int tata[MAX_N + 5];
static inline bool find_paths(){
    queue<int> path;
    for(int i=1; i<=n; i++)
        tata[i] = -1;

    tata[1] = 0;
    path.push(1);

    int crt;
    while(!path.empty()){
        crt = path.front();
        path.pop();

        for(auto nxt : edge[crt])
            if(tata[nxt] == -1 && flow[crt][nxt] < cap[crt][nxt]){
                tata[nxt] = crt;
                if(nxt != n)
                    path.push(nxt);
            }
    }

    if(tata[n] != -1)
        return true;
    return false;
}

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);

    fin>>n>>m;
    for(int i=1, u, v, c; i<=m; i++){
        fin>>u>>v>>c;
        edge[u].push_back(v);
        edge[v].push_back(u);
        cap[u][v] = c;
    }

    int flux = 0, minflow, nod;
    while(find_paths()){
        for(auto crt : edge[n]){
            if(tata[crt] != -1 && flow[crt][n] < cap[crt][n]){
                minflow = INT_MAX;
                tata[n] = crt;

                nod = n;
                while(nod != 1){
                    minflow = min(minflow, cap[tata[nod]][nod] - flow[tata[nod]][nod]);
                    nod = tata[nod];
                }

                nod = n;
                while(nod != 1){
                    flow[tata[nod]][nod] += minflow;
                    flow[nod][tata[nod]] -= minflow;
                    nod = tata[nod];
                }

                flux += minflow;
            }
        }
    }
    fout<<flux;
    return 0;
}
/**
Input:

**/