Cod sursa(job #3336639)

Utilizator DinVinEmanuel DinVin Data 25 ianuarie 2026 03:06:30
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
//flux cu ford-fulkerson
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout ("maxflow.out");
const int INF = 1e9;
vector<vector<int>> M;
int cap[1001][1001];
int n,m;
vector<bool> viz;
int dfs(int u, int t, int flow) {
    if (u==t) return flow;
    viz[u]=true;

    for (auto v : M[u]) {
        if (!viz[v] && cap[u][v] > 0) {
            int pushed = dfs(v,t,min(flow, cap[u][v]));

            if (pushed > 0) {
                cap[u][v] -= pushed;
                cap[v][u] += pushed;
                return pushed;
            }
        }
    }
    return 0;
}

int main() {
    fin>>n>>m;
    M.resize(n+1);
    for (int i=1;i<=m;i++) {
        int x,y,z;
        fin>>x>>y>>z;
        M[x].push_back(y);
        M[y].push_back(x);
        cap[x][y] = z;
    }
    int max_flow = 0;
    int s = 1;
    int t = n;
    while (true) {
        viz.assign(n+1,false);
        int pushed = dfs(s,t,INF);
        if (pushed == 0)break;
        max_flow += pushed;
    }
    fout<<max_flow;

    return 0;
}