結果

問題 No.1301 Strange Graph Shortest Path
ユーザー scol_kpscol_kp
提出日時 2020-11-28 00:37:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 3,000 ms
コード長 7,346 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 218,884 KB
実行使用メモリ 38,220 KB
最終ジャッジ日時 2023-10-09 22:58:31
合計ジャッジ時間 9,015 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 136 ms
35,576 KB
testcase_03 AC 113 ms
31,924 KB
testcase_04 AC 168 ms
34,472 KB
testcase_05 AC 115 ms
35,240 KB
testcase_06 AC 148 ms
32,052 KB
testcase_07 AC 147 ms
34,188 KB
testcase_08 AC 117 ms
32,828 KB
testcase_09 AC 144 ms
30,640 KB
testcase_10 AC 117 ms
32,796 KB
testcase_11 AC 157 ms
33,636 KB
testcase_12 AC 157 ms
33,168 KB
testcase_13 AC 142 ms
36,188 KB
testcase_14 AC 139 ms
31,580 KB
testcase_15 AC 139 ms
31,568 KB
testcase_16 AC 175 ms
34,480 KB
testcase_17 AC 152 ms
36,376 KB
testcase_18 AC 135 ms
33,668 KB
testcase_19 AC 151 ms
32,524 KB
testcase_20 AC 153 ms
31,148 KB
testcase_21 AC 145 ms
34,884 KB
testcase_22 AC 154 ms
32,440 KB
testcase_23 AC 145 ms
36,252 KB
testcase_24 AC 158 ms
32,372 KB
testcase_25 AC 168 ms
34,668 KB
testcase_26 AC 144 ms
33,144 KB
testcase_27 AC 154 ms
33,964 KB
testcase_28 AC 124 ms
34,968 KB
testcase_29 AC 173 ms
33,900 KB
testcase_30 AC 165 ms
34,592 KB
testcase_31 AC 170 ms
34,068 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 87 ms
30,088 KB
testcase_34 AC 162 ms
38,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();

template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <ostream& os = std::cout>
class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
        template <typename T>
        auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
            for (size_t i = 0; i < sz; i++)
                os << vec[i] << (i == sz - 1 ? term : sep);
        }
        template <typename T>
        auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
            for (size_t i = 0; i < h; i++)
                for (size_t j = 0; j < w; j++)
                    os << mat[i][j] << (j == w - 1 ? term : sep);
        }

    private:
        const char *sep, *term;
        void print() const { os << term; }
        void print_rest() const { os << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints<> prints;
Prints<std::cerr> prints_err;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

template <typename Cap, typename Cost>
class MinCostFlowGraph {
private:
    struct _Edge {
        int to, rev;
        Cap cap;
        Cost cost;
        _Edge() noexcept {}
        _Edge(int to, Cap cap, Cost cost, int rev) noexcept
            : to(to), rev(rev), cap(cap), cost(cost) {}
    };
    std::vector<std::vector<_Edge>> g;
    std::vector<std::pair<int, int>> pos;

public:
    MinCostFlowGraph(int n) noexcept : g(n) {}
    int add_edge(int from, int to, Cap cap, Cost cost) noexcept {
        const int m = static_cast<int>(pos.size());
        const int from_id = static_cast<int>(g[from].size());
        int to_id = static_cast<int>(g[to].size());
        pos.emplace_back(from, static_cast<int>(g[from].size()));
        if (from == to) ++to_id;
        g[from].emplace_back(to, cap, cost, to_id);
        g[to].emplace_back(from, 0, -cost, from_id);
        return m;
    }
    struct Edge {
        int from, to;
        Cap cap, flow;
        Cost cost;
        Edge() noexcept {}
        Edge(int from, int to, Cap cap, Cap flow, Cost cost) noexcept
            : from(from), to(to), cap(cap), flow(flow), cost(cost) {}
    };
    Edge get_edge(int i) const noexcept {
        const auto e = g[pos[i].first][pos[i].second];
        const auto re = g[e.to][e.rev];
        return Edge(pos[i].first, e.to, e.cap + re.cap, re.cap, e.cost);
    }
    std::vector<Edge> edges() const noexcept {
        const int m = static_cast<int>(pos.size());
        std::vector<Edge> res;
        res.reserve(m);
        for (int i = 0; i < m; i++) res.emplace_back(get_edge(i));
        return res;
    }
    std::pair<Cap, Cost> flow(int s, int t) noexcept {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) noexcept {
        return slope(s, t, flow_limit).back();
    }
    std::vector<std::pair<Cap, Cost>> slope(int s, int t) noexcept {
        return slope(s, t, std::numeric_limits<Cap>::max());
    }
    std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) noexcept {
        constexpr Cost C_INF = std::numeric_limits<Cost>::max();
        const int n = static_cast<int>(g.size());
        std::vector<Cost> h(n), dist(n);
        std::vector<int> prevv(n), preve(n);
        std::vector<std::pair<Cap, Cost>> res;
        struct Q {
            Cost key;
            int to;
            Q(Cost key, int to)
            noexcept : key(key), to(to) {}
            bool operator>(const Q& r) const noexcept { return key > r.key; }
        };
        Cap flow = 0;
        Cost cost = 0, prev_cost = -1;
        res.emplace_back(flow, cost);
        while (flow < flow_limit) {
            std::priority_queue<Q, std::vector<Q>, std::greater<Q>> que;
            dist.assign(n, C_INF);
            dist[s] = 0;
            que.emplace(0, s);
            while (!que.empty()) {
                auto [d, v] = que.top();
                que.pop();
                if (dist[v] < d) continue;
                const int m = static_cast<int>(g[v].size());
                for (int i = 0; i < m; i++) {
                    const _Edge& e = g[v][i];
                    Cost cn = dist[v] + e.cost + h[v] - h[e.to];
                    if (e.cap > 0 && dist[e.to] > cn) {
                        dist[e.to] = cn;
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        que.emplace(cn, e.to);
                    }
                }
            }
            if (dist[t] == C_INF) break;
            for (int i = 0; i < n; i++) h[i] += dist[i];
            Cap f = flow_limit - flow;
            for (int i = t; i != s; i = prevv[i]) {
                f = std::min(f, g[prevv[i]][preve[i]].cap);
            }
            for (int i = t; i != s; i = prevv[i]) {
                _Edge& e = g[prevv[i]][preve[i]];
                e.cap -= f;
                g[i][e.rev].cap += f;
            }
            Cost d = h[t];
            flow += f;
            cost += f * d;
            if (prev_cost == d) res.pop_back();
            res.emplace_back(flow, cost);
            prev_cost = d;
        }
        return res;
    }
};

void solve() {
    int N, M;
    cin >> N >> M;
    MinCostFlowGraph<ll, ll> mcf(N);
    for (ll i = 0; i < M; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        --a, --b;
        mcf.add_edge(a, b, 1, c);
        mcf.add_edge(a, b, 1, d);
        mcf.add_edge(b, a, 1, c);
        mcf.add_edge(b, a, 1, d);
    }
    auto flw = mcf.flow(0, N - 1, 2);
    prints()(flw.second);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    std::ifstream ifs("./in_out/input.txt");
    std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    std::ofstream ofs("./in_out/output.txt");
    std::cout.rdbuf(ofs.rdbuf());
#endif
    std::cout << std::setprecision(18) << std::fixed;
    solve();
    std::cout << std::flush;
    return 0;
}
0