結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー Ankit KumarAnkit Kumar
提出日時 2023-01-23 02:58:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 3,285 bytes
コンパイル時間 1,327 ms
コンパイル使用メモリ 103,172 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-07 09:02:55
合計ジャッジ時間 5,905 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 13 ms
4,380 KB
testcase_08 AC 15 ms
4,380 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 21 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 4 ms
4,384 KB
testcase_15 AC 19 ms
4,380 KB
testcase_16 AC 22 ms
4,376 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 21 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 9 ms
4,380 KB
testcase_44 AC 9 ms
4,380 KB
testcase_45 AC 15 ms
4,380 KB
testcase_46 AC 15 ms
4,380 KB
testcase_47 AC 12 ms
4,380 KB
testcase_48 AC 11 ms
4,376 KB
testcase_49 AC 11 ms
4,376 KB
testcase_50 AC 11 ms
4,380 KB
testcase_51 AC 10 ms
4,388 KB
testcase_52 AC 10 ms
4,504 KB
testcase_53 AC 17 ms
4,380 KB
testcase_54 AC 18 ms
4,380 KB
testcase_55 AC 19 ms
4,376 KB
testcase_56 AC 18 ms
4,376 KB
testcase_57 AC 18 ms
4,380 KB
testcase_58 AC 17 ms
4,380 KB
testcase_59 AC 18 ms
4,380 KB
testcase_60 AC 17 ms
4,376 KB
testcase_61 AC 17 ms
4,376 KB
testcase_62 AC 19 ms
4,380 KB
testcase_63 AC 16 ms
4,376 KB
testcase_64 AC 19 ms
4,384 KB
testcase_65 AC 21 ms
4,380 KB
testcase_66 AC 21 ms
4,380 KB
testcase_67 AC 20 ms
4,376 KB
testcase_68 AC 19 ms
4,376 KB
testcase_69 AC 19 ms
4,380 KB
testcase_70 AC 21 ms
4,376 KB
testcase_71 AC 21 ms
4,380 KB
testcase_72 AC 20 ms
4,380 KB
testcase_73 AC 22 ms
4,388 KB
testcase_74 AC 23 ms
4,380 KB
testcase_75 AC 3 ms
4,380 KB
testcase_76 AC 3 ms
4,380 KB
testcase_77 AC 3 ms
4,384 KB
testcase_78 AC 20 ms
4,376 KB
testcase_79 AC 21 ms
4,380 KB
testcase_80 AC 22 ms
4,380 KB
testcase_81 AC 11 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:67:15: 警告: comma-separated list in using-declaration only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   67 | using std::cin, std::cout, std::vector;
      |               ^
main.cpp:67:26: 警告: comma-separated list in using-declaration only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   67 | using std::cin, std::cout, std::vector;
      |                          ^

ソースコード

diff #

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>
#include <atcoder/maxflow>



// LinearProgrammingOnBasePolyhedron : Maximize/minimize linear function on base polyhedron, using Edmonds' algorithm
//
// maximize/minimize cx s.t. (x on some base polyhedron)
// Reference: <https://www.amazon.co.jp/dp/B01N6G0579>, Sec. 2.4, Algorithm 2.2-2.3
//            "Submodular Functions, Matroids, and Certain Polyhedra" [Edmonds+, 1970]
template <typename Tvalue> struct LinearProgrammingOnBasePolyhedron {
    using Tfunc = std::function<Tvalue(int, const std::vector<Tvalue> &)>;
    static Tvalue EPS;
    int N;
    std::vector<Tvalue> c;
    Tfunc maximize_xi;
    Tvalue xsum;
    bool minimize;

    Tvalue fun;
    std::vector<Tvalue> x;
    bool infeasible;

    void _init(const std::vector<Tvalue> &c_, Tfunc q_, Tvalue xsum_, Tvalue xlowerlimit, bool minimize_) {
        N = c_.size();
        c = c_;
        maximize_xi = q_;
        xsum = xsum_;
        minimize = minimize_;
        fun = 0;
        x.assign(N, xlowerlimit);
        infeasible = false;
    }

    void _solve() {
        std::vector<std::pair<Tvalue, int>> c2i(N);
        for (int i = 0; i < N; i++) c2i[i] = std::make_pair(c[i], i);

        std::sort(c2i.begin(), c2i.end());
        if (!minimize) std::reverse(c2i.begin(), c2i.end());
        for (const auto &p : c2i) {
            const int i = p.second;
            x[i] = maximize_xi(i, x);
        }
        if (std::abs(std::accumulate(x.begin(), x.end(), Tvalue(0)) - xsum) > EPS) {
            infeasible = true;
        } else {
            for (int i = 0; i < N; i++) fun += x[i] * c[i];
        }
    }

    LinearProgrammingOnBasePolyhedron(const std::vector<Tvalue> &c_, Tfunc q_, Tvalue xsum_, Tvalue xlowerlimit, bool minimize_) {
        _init(c_, q_, xsum_, xlowerlimit, minimize_);
        _solve();
    }
};
template <> long long LinearProgrammingOnBasePolyhedron<long long>::EPS = 0;
template <> long double LinearProgrammingOnBasePolyhedron<long double>::EPS = 1e-12;

using std::cin, std::cout, std::vector;

int main() {
    using Num = long long;
    int N, M;
    long long K;
    cin >> N >> M >> K;
    vector<int> A(M), B(M);
    vector<Num> C(M), D(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i] >> D[i];
        A[i]--, B[i]--;
    }

    auto maximize_xi = [&](int ie, const vector<Num> &xnow) -> Num {
        atcoder::mf_graph<Num> mf(N + 2);
        mf.add_edge(N, A[ie], 2 * K * N);
        mf.add_edge(N, B[ie], 2 * K * N);
        for (int je = 0; je < M; je++) {
            mf.add_edge(A[je], B[je], xnow[je]);
            mf.add_edge(B[je], A[je], xnow[je]);
            mf.add_edge(N, A[je], xnow[je]);
            mf.add_edge(N, B[je], xnow[je]);
        }
        for (int iv = 0; iv < N; iv++) mf.add_edge(iv, N + 1, 2 * K);
        Num ret = mf.flow(N, N + 1) / 2 - K - std::accumulate(xnow.begin(), xnow.end(), (Num)0);
        return std::min(ret, D[ie]);
    };

    LinearProgrammingOnBasePolyhedron<Num> solver(C, maximize_xi, K * (N - 1), 0, true);
    if (solver.infeasible) {
        cout << "-1\n";
    } else {
        cout << (long long)solver.fun << '\n';
    }
}
0