Language: C++

#include <bits/stdc++.h>


#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

const int INF = 1L << 30;
typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }



class CancelingNegativeCycles {
    struct Edge {
        const int to;          // 行き先のノードid
        long long flow;        // 流量
        const long long cap;   // 容量
        const long long cost;  // cost
        const int rev;         // 逆辺のノードid
        const bool is_rev;     // 逆辺かどうか
        Edge(int to, long long flow, long long cap, long long cost, int rev, bool is_rev) : to(to), flow(flow), cost(cost), cap(cap), rev(rev), is_rev(is_rev) {
            assert(this->cap >= 0);
        }
    };

    const unsigned int num_node;          // 頂点数
    std::vector<std::vector<Edge>> graph; // グラフの隣接リスト表現

    // dinic用
    std::vector<int> level;               // sからの距離
    std::vector<unsigned int> iter;       // どこまで調べ終わったか

public:
    CancelingNegativeCycles(const unsigned int num_node) : num_node(num_node) {
        graph.resize(num_node);
        level.resize(num_node);
        iter.resize(num_node);
    }

    // fromからtoへ向かう容量cap、コストcostの辺をグラフに追加する
    void add_edge(const unsigned int from, const unsigned int to, const long long cap, const long long cost) {
        graph.at(from).emplace_back(Edge(to, 0, cap, cost, graph.at(to).size(), false));
        graph.at(to).emplace_back(Edge(from, cap, cap, -cost, graph.at(from).size() - 1, true));
    }

    // sからtへの流量fの最小費用流を求める
    // 流せない場合は-1を返す
    int min_cost_flow(const unsigned int source, const unsigned int sink, long long flow) {

        // 最大流を求める
        int can_flow = max_flow(source, sink, flow);
        if (not can_flow) {
            return -1;
        }

        while (true) {

            // bellman-fordでsinkからの最短路を求める
            std::vector<int> prev_v(num_node, -1), prev_e(num_node, -1); // 直前の頂点と辺のidx
            std::vector<long long> distance(num_node, LONG_LONG_MAX);
            distance[sink] = 0;

            bool have_negative_cycle = false;
            for (int num = 0; num < num_node; ++num) {
                for (int u = 0; u < graph.size(); ++u) {
                    for (int i = 0; i < graph.at(u).size(); ++i) {
                        Edge &e = graph.at(u).at(i);

                        if (distance.at(u) == LONG_LONG_MAX) {
                            continue;
                        }

                        long long new_dist = distance.at(u) + e.cost;
                        if (e.cap - e.flow > 0 and distance.at(e.to) > new_dist) {
                            distance.at(e.to) = new_dist;
                            prev_v.at(e.to) = u;
                            prev_e.at(e.to) = i;

                            if (num == num_node - 1) {
                                have_negative_cycle = true;
                            }
                        }
                    }
                }
            }

            // sinkから到達できる箇所に閉路がない
            if (not have_negative_cycle) {
                break;
            }

            long long d = LONG_LONG_MAX;
            std::vector<bool> used(num_node, false);
            int u = sink;
            while (not used.at(u)) {
                used.at(u) = true;
                const Edge &e = graph.at(prev_v.at(u)).at(prev_e.at(u));
                d = std::min(d, e.cap - e.flow);
                u = prev_v.at(u);
            }

            assert(d != 0);

            std::fill(used.begin(), used.end(), false);
            // 閉路の開始点から見ていく
            while (not used.at(u)) {
                used.at(u) = true;
                Edge &e = graph.at(prev_v.at(u)).at(prev_e.at(u));
                e.flow += d;
                graph.at(e.to).at(e.rev).flow -= d;
                u = prev_v.at(u);
            }
        }

        int cost = 0;
        for (int u = 0; u < graph.size(); ++u) {
            for (int i = 0; i < graph.at(u).size(); ++i) {
                Edge &e = graph.at(u).at(i);
                if (not e.is_rev) {
                    cost += e.flow * e.cost;
                }
            }
        }

        return cost;
    }

private:
    // sからtへflowだけdinicで流す
    bool max_flow(unsigned int s, unsigned int t, int flow) {
        while (flow > 0) {
            bfs(s);
            if (level.at(t) < 0) {
                break;
            }

            std::fill(iter.begin(), iter.end(), 0);
            long long f;
            while ((f = dfs(s, t, flow)) > 0) {
                flow -= f;
            }
        }
        return flow == 0;
    }

    // sからの最短距離をBFSで計算する
    void bfs(unsigned int s) {
        std::fill(level.begin(), level.end(), -1);
        std::queue<unsigned int> que;
        level.at(s) = 0;
        que.push(s);
        while (not que.empty()) {
            unsigned int v = que.front();
            que.pop();
            for (int i = 0; i < graph.at(v).size(); ++i) {
                Edge &e = graph.at(v).at(i);
                if ((e.cap - e.flow) > 0 and level.at(e.to) < 0) {
                    level.at(e.to) = level.at(v) + 1;
                    que.push(e.to);
                }
            }
        }
    }

    // 増加パスをDFSで探す
    long long dfs(unsigned int v, unsigned int t, long long f) {
        if (v == t) {
            return f;
        }
        for (unsigned int &i = iter.at(v); i < graph.at(v).size(); ++i) {
            Edge &e = graph.at(v).at(i);
            if ((e.cap - e.flow) > 0 and level.at(v) < level.at(e.to)) {
                long long d = dfs(e.to, t, min(f, e.cap - e.flow));
                if (d > 0) {
                    e.flow += d;
                    graph.at(e.to).at(e.rev).flow -= d;
                    return d;
                }
            }
        }
        return 0;
    }
};

using namespace std;

int main() {

    int V, E, F;
    cin >> V >> E >> F;
    CancelingNegativeCycles mcf(V);
    for (int i = 0; i < E; i++) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        mcf.add_edge(u, v, c, d);
    }

    cout << mcf.min_cost_flow(0, V - 1, F) << endl;

    return 0;
}


Compiler output:
a.cc: In member function 'long long int CancelingNegativeCycles::dfs(unsigned int, unsigned int, long long int)':
a.cc:191:44: error: 'min' was not declared in this scope; did you mean 'std::min'?
  191 |                 long long d = dfs(e.to, t, min(f, e.cap - e.flow));
      |                                            ^~~
      |                                            std::min
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~