Cod sursa(job #2925425)

Utilizator rares89_Dumitriu Rares rares89_ Data 15 octombrie 2022 11:06:51
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
 
using namespace std;
 
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
 
int n, x, y, z;
vector<vector<int>> C, G;
vector<int> root;
 
bool bfs(int start) {
    queue<int> Q;
    fill(root.begin(), root.end(), 0);
    Q.push(start);
    while(!Q.empty()) {
        int nod = Q.front();
        Q.pop();
        for(auto i : G[nod]) {
            if(root[i] == 0 && C[nod][i] > 0) {
                root[i] = nod;
                Q.push(i);
                if(i == n) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
int eKarp(int start) {
    int flow = 0;
    while(bfs(start)) {
        int minim = 2e9, i = n;
        while(i != start) {
            minim = min(minim, C[root[i]][i]);
            i = root[i];
        }
        flow += minim;
        i = n;
        while(i != start) {
            C[root[i]][i] -= minim;
            C[i][root[i]] += minim;
            i = root[i];
        }
    }
    return flow;
}
 
int main() {
    fin >> n;
    root.resize(1LL * n + 1);
    G.resize(1LL * n + 1);
    C.resize(1LL * n + 1);
    for(int i = 1; i <= n; i++) {
        C[i].resize(1LL * n + 1);
    }
    while(fin >> x >> y >> z) {
        G[x].push_back(y);
        G[y].push_back(x);
        C[x][y] = z;
    }
    int flow = eKarp(1);
    fout << (flow == 0 ? "Apa nu ajunge!" : to_string(flow));
    return 0;
}