compro-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ningenMe/compro-library

:heavy_check_mark: test/graph/PrimalDualMinCostFlow.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B"

#include <vector>
#include <iostream>
#include <queue>
#include <cassert>
using namespace std;
#include "../../lib/00-util/FastIO.cpp"
#include "../../lib/40-graph/PrimalDualMinCostFlow.cpp"

int main() {
	cin.tie(0);ios::sync_with_stdio(false);
    int N,M,F;
    read(N),read(M),read(F);
    PrimalDualMinCostFlow<int,int> pdmcf(N, 123456789);
    for(int i=0;i<M;++i) {
        int u,v,c,d;
        read(u),read(v),read(c),read(d);
        pdmcf.make_edge(u,v,c,d);
    }
    auto [flow,cost] = pdmcf.min_cost_flow(0,N-1,F);
    if(flow < F) cost = -1;
    cout << cost << endl;
    return 0;
}
#line 1 "test/graph/PrimalDualMinCostFlow.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B"

#include <vector>
#include <iostream>
#include <queue>
#include <cassert>
using namespace std;
#line 1 "lib/00-util/FastIO.cpp"
/*
 * @title FastIO
 * @docs md/util/FastIO.md
 */
class FastIO{
private:
    inline static constexpr int ch_0='0';
    inline static constexpr int ch_9='9';
    inline static constexpr int ch_n='-';
    inline static constexpr int ch_s=' ';
    inline static constexpr int ch_l='\n';
    inline static void endline_skip(char& ch) {
        while(ch==ch_l) ch=getchar();
    }
    template<typename T> inline static void read_integer(T &x) {
        int neg=0; char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        if(ch==ch_n) neg=1,ch=getchar();
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
        if(neg) x*=-1;
    }
    template<typename T> inline static void read_unsigned_integer(T &x) {
        char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
    }
    inline static void read_string(string &x) {
        char ch; x="";
        ch=getchar();
        endline_skip(ch);
        for(;(ch != ch_s && ch!=ch_l); ch = getchar()) x.push_back(ch);
    }
    inline static char ar[40];
    inline static char *ch_ar;
    template<typename T> inline static void write_integer(T x) {
        ch_ar=ar;
        if(x< 0) putchar(ch_n), x=-x;
        if(x==0) putchar(ch_0);
        for(;x;x/=10) *ch_ar++=(ch_0+x%10);
        while(ch_ar--!=ar) putchar(*ch_ar);
    }
public:
    inline static void read(int &x) {read_integer<int>(x);}
    inline static void read(long long &x) {read_integer<long long>(x);}
    inline static void read(unsigned int &x) {read_unsigned_integer<unsigned int>(x);}
    inline static void read(unsigned long long &x) {read_unsigned_integer<unsigned long long>(x);}
    inline static void read(string &x) {read_string(x);}
    inline static void read(__int128_t &x) {read_integer<__int128_t>(x);}
    inline static void write(__int128_t x) {write_integer<__int128_t>(x);}
    inline static void write(char x) {putchar(x);}
};
#define read(arg) FastIO::read(arg)
#define write(arg) FastIO::write(arg)
#line 1 "lib/40-graph/PrimalDualMinCostFlow.cpp"
/*
 * @title PrimalDualMinCostFlow - 最短路反復の最小費用流
 * @docs md/graph/PrimalDualMinCostFlow.md
 */
template<class TypeFlow, class TypeCost> class PrimalDualMinCostFlow {
    using Pair = pair<TypeCost,size_t>;
    struct Edge {
        size_t to;
        size_t rev;
        TypeFlow cap;
        TypeCost cost; 
    };
    vector<vector<Edge>> edge;
    const size_t N;
    const TypeCost inf_cost;
    vector<TypeCost> min_cost;
    vector<TypeCost> potential;
    vector<size_t> prev_vertex,prev_edge;
    TypeFlow max_flow=0;
public:
    PrimalDualMinCostFlow(const size_t N, const TypeCost inf_cost) 
        : N(N), edge(N), min_cost(N), potential(N,0), prev_vertex(N,N), prev_edge(N,N), inf_cost(inf_cost) {}
    // costは単位流量あたりのコスト
    inline void make_edge(const size_t from, const size_t to, const TypeFlow cap, const TypeCost cost) {
        assert(cost < inf_cost);
        edge[from].push_back({ to, edge[to].size(), cap, cost });
        edge[to].push_back({ from, edge[from].size() - 1, 0, -cost });
        max_flow += cap;
    }
    pair<TypeFlow,TypeCost> min_cost_flow(const size_t s, const size_t g) {
        return min_cost_flow(s,g,max_flow);
    }
    pair<TypeFlow,TypeCost> min_cost_flow(const size_t s, const size_t g, const TypeFlow limit_flow) {
        assert(0 <= s && s < N && 0 <= g && g < N && s != g);
        priority_queue<Pair,vector<Pair>,greater<Pair>> pq;

        TypeCost sum_cost=0;
        TypeFlow sum_flow=0;
        while(sum_flow < limit_flow) {
            min_cost.assign(N, inf_cost);
            {
                pq.emplace(0,s);
                min_cost[s]=0;
            }
            while(pq.size()) {
                auto [from_cost, from] = pq.top(); pq.pop();
                if(min_cost[from] < from_cost) continue;

                for(int i=0; i < edge[from].size(); ++i) {
                    auto [to, rev, cap, cost] = edge[from][i];
                    TypeCost to_cost = from_cost + cost + (potential[from] - potential[to]);
                    if(cap > 0 && min_cost[to] > to_cost) {
                        pq.emplace(to_cost, to);
                        prev_vertex[to] = from;
                        prev_edge[to] = i;
                        min_cost[to] = to_cost;
                    }
                }
            }
            if(min_cost[g]==inf_cost) break;
            for(size_t i=0; i<N; ++i) potential[i] += min_cost[i];

            TypeFlow diff_flow = limit_flow - sum_flow;
            for(size_t i=g; i!=s; i = prev_vertex[i]) {
                diff_flow = min(diff_flow, edge[prev_vertex[i]][prev_edge[i]].cap);
            }
            sum_flow += diff_flow;
            sum_cost += diff_flow * potential[g];
            for(size_t i=g; i!=s; i = prev_vertex[i]) {
                auto& [_to,rev,cap,_cost] = edge[prev_vertex[i]][prev_edge[i]];
                auto& [_r_to,_r_rev,r_cap,_r_cost] = edge[i][rev];

                cap -= diff_flow;
                r_cap += diff_flow;
            }
        }
        return {sum_flow, sum_cost};
    }

};
#line 10 "test/graph/PrimalDualMinCostFlow.test.cpp"

int main() {
	cin.tie(0);ios::sync_with_stdio(false);
    int N,M,F;
    read(N),read(M),read(F);
    PrimalDualMinCostFlow<int,int> pdmcf(N, 123456789);
    for(int i=0;i<M;++i) {
        int u,v,c,d;
        read(u),read(v),read(c),read(d);
        pdmcf.make_edge(u,v,c,d);
    }
    auto [flow,cost] = pdmcf.min_cost_flow(0,N-1,F);
    if(flow < F) cost = -1;
    cout << cost << endl;
    return 0;
}
Back to top page