Cod sursa(job #3269677)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 20 ianuarie 2025 13:50:22
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define fi first
#define sc second
#define pb push_back

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
template<typename type>
using ordered_set = tree<type, null_type, less<type>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1005, mod = 1e9 + 7, inf = 2e9;
const int dl[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
const int ddl[] = {-1, -1, -1, 0, 1, 1, 1, 0}, ddc[] = {-1, 0, 1, 1, 1, 0, -1, -1};

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
int rng(int lo = 1, int hi = INT_MAX) {
    uniform_int_distribution<int> rnd(lo, hi);
    return rnd(gen);
}

struct mint {
    int val;
    mint(int32_t x = 0) {
        val = x % mod;
    }
    mint(long long x) {
        val = x % mod;
    }
    mint operator+(mint x) {
        return val + x.val;
    }
    mint operator-(mint x) {
        return val - x.val + mod;
    }
    mint operator*(mint x) {
        return 1LL * val * x.val;
    }
    void operator+=(mint x) {
        val = (*this + x).val;
    }
    void operator-=(mint x) {
        val = (*this - x).val;
    }
    void operator*=(mint x) {
        val = (*this * x).val;
    }
    friend auto operator>>(istream& in, mint &x) -> istream& {
        in >> x.val;
        x.val %= mod;
        return in;
    }
    friend auto operator<<(ostream& out, mint const &x) -> ostream& {
        out << x.val;
        return out;
    }
};

struct edge {
    int nxt, flow, cap, inv;
};

int n, m, niv[N], start[N];
vector<edge> g[N];

bool bfs() {
    memset(niv, 0, sizeof(niv));

    niv[1] = 1;
    queue<int> que;
    que.push(1);
    while(!que.empty()) {
        int nod = que.front();
        que.pop();

        for(auto &[nxt, flow, cap, inv] : g[nod])
            if(!niv[nxt] && flow < cap) {
                niv[nxt] = niv[nod] + 1;
                que.push(nxt);
            }
    }

    return niv[n];
}
int sendflow(int nod, int flow) {
    if(nod == n)
        return flow;

    for(; start[nod]<g[nod].size(); start[nod]++) {
        auto [nxt, edgeflow, cap, inv] = g[nod][start[nod]];
        if(niv[nxt] == niv[nod] + 1 && edgeflow < cap) {
            int flownow = min(flow, cap - edgeflow);
            int nxtflow = sendflow(nxt, flownow);
            if(nxtflow > 0) {
                g[nod][start[nod]].flow += nxtflow;
                g[nxt][inv].flow -= nxtflow;

                return nxtflow;
            }
        }
    }

    return 0;
}
int dinic() {
    int ans = 0;
    while(bfs()) {
        memset(start, 0, sizeof(start));

        int rez;
        while(rez = sendflow(1, inf))
            ans += rez;
    }

    return ans;
}

int32_t main() {
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    cin.tie(nullptr)->sync_with_stdio(0);

    cin >> n >> m;
    for(int i=1; i<=m; i++) {
        int x, y, c;
        cin >> x >> y >> c;
        g[x].pb({y, 0, c, int(g[y].size())});
        g[y].pb({x, 0, 0, int(g[x].size()-1)});
    }

    cout << dinic();
    return 0;
}