Cod sursa(job #3337123)

Utilizator anon123andrei popescu anon123 Data 26 ianuarie 2026 22:29:16
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

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

constexpr int NMAX = 1005;
constexpr int INF = 1e7;
vector<vector<int>> lista;
int C[NMAX][NMAX], F[NMAX][NMAX];
vector<int>tata;
vector<bool> viz;

bool bfs(int s, int d) {
    fill(viz.begin(), viz.end(), false);
    fill(tata.begin(), tata.end(), -1);

    queue<int> q;
    q.push(s);
    viz[s] = true;
    tata[s] = 0;

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

        if (u == d) return true;

        for (int v : lista[u]) {
            if (!viz[v] && C[u][v] - F[u][v] > 0) {
                q.push(v);
                viz[v] = true;
                tata[v] = u;
            }
        }
    }


    return false;
}

int main(){
    int n,m;
    fin>>n>>m;
    lista.resize(n+1);
    tata.resize(n+1);
    viz.resize(n+1);
    for (int i = 0; i<m;i++) {
        int u,v,c;
        fin>>u>>v>>c;
        lista[u].push_back(v);
        lista[v].push_back(u);
        C[u][v] = c;
    }

    int maxfloww = 0;
    while (bfs(1,n)) {
        int pathflow = INF;

        for (int v = n; v != 1; v=tata[v]) {
            int u = tata[v];
            pathflow = min(pathflow, C[u][v] - F[u][v]); // cred
        }

        for (int v = n; v!=1; v=tata[v]) {
            int u = tata[v];
            F[u][v] += pathflow;
            F[v][u] -= pathflow;
        }
        maxfloww += pathflow;
    }
    fout<<maxfloww;
    return 0;
}