Cod sursa(job #3336283)

Utilizator anon123andrei popescu anon123 Data 24 ianuarie 2026 15:17:32
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
constexpr int NMAX = 1005;
constexpr int INF = 1e9;
vector<int> adj[NMAX];
int c[NMAX][NMAX];
int f[NMAX][NMAX];
int n,m;
int parent[NMAX];

bool bfs(int s, int d) {
    memset(parent,0,sizeof(parent));
    queue<int> q;

    q.push(s);
    parent[s] = -1;

    while (!q.empty()) {
        int u = q.front();
        q.pop();

        if (u == d) return true;

        for (int v : adj[u]) {
            if (parent[v] == 0 && c[u][v] - f[u][v] > 0) {
                parent[v] = u;
                q.push(v);
            }
        }
    }
    return false;
}

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

    while (bfs(1,n)) {
        int path_flow = INF;

        for (int v = n; v!=1; v=parent[v]) {
            int u = parent[v];
            path_flow = min(path_flow, c[u][v] - f[u][v]);
        }

        for (int v = n; v!=1; v = parent[v]) {
            int u = parent[v];
            f[u][v] += path_flow;
            f[v][u] -= path_flow;
        }
        max_flow += path_flow;
    }
    fout<<max_flow;
    return 0;
}