結果
問題 | No.1316 Maximum Minimum Spanning Tree |
ユーザー | hitonanode |
提出日時 | 2020-12-12 01:47:03 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,709 bytes |
コンパイル時間 | 1,806 ms |
コンパイル使用メモリ | 138,084 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-19 22:05:36 |
合計ジャッジ時間 | 5,344 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | WA | - |
testcase_08 | AC | 29 ms
6,944 KB |
testcase_09 | AC | 44 ms
6,940 KB |
testcase_10 | AC | 41 ms
6,940 KB |
testcase_11 | WA | - |
testcase_12 | AC | 6 ms
6,940 KB |
testcase_13 | AC | 6 ms
6,944 KB |
testcase_14 | AC | 8 ms
6,940 KB |
testcase_15 | WA | - |
testcase_16 | AC | 46 ms
6,940 KB |
testcase_17 | AC | 49 ms
6,944 KB |
testcase_18 | AC | 48 ms
6,944 KB |
testcase_19 | AC | 3 ms
6,940 KB |
testcase_20 | AC | 3 ms
6,940 KB |
testcase_21 | AC | 3 ms
6,940 KB |
testcase_22 | AC | 3 ms
6,944 KB |
testcase_23 | AC | 4 ms
6,940 KB |
testcase_24 | AC | 4 ms
6,940 KB |
testcase_25 | AC | 4 ms
6,944 KB |
testcase_26 | AC | 4 ms
6,940 KB |
testcase_27 | AC | 3 ms
6,944 KB |
testcase_28 | AC | 3 ms
6,944 KB |
testcase_29 | AC | 3 ms
6,944 KB |
testcase_30 | AC | 3 ms
6,940 KB |
testcase_31 | AC | 4 ms
6,948 KB |
testcase_32 | AC | 4 ms
6,940 KB |
testcase_33 | AC | 5 ms
6,940 KB |
testcase_34 | AC | 4 ms
6,944 KB |
testcase_35 | WA | - |
testcase_36 | AC | 3 ms
6,944 KB |
testcase_37 | AC | 3 ms
6,940 KB |
testcase_38 | AC | 3 ms
6,944 KB |
testcase_39 | WA | - |
testcase_40 | AC | 5 ms
6,944 KB |
testcase_41 | AC | 4 ms
6,940 KB |
testcase_42 | AC | 4 ms
6,940 KB |
testcase_43 | WA | - |
testcase_44 | AC | 14 ms
6,944 KB |
testcase_45 | AC | 32 ms
6,944 KB |
testcase_46 | AC | 30 ms
6,944 KB |
testcase_47 | AC | 28 ms
6,940 KB |
testcase_48 | AC | 29 ms
6,944 KB |
testcase_49 | AC | 17 ms
6,940 KB |
testcase_50 | AC | 24 ms
6,940 KB |
testcase_51 | WA | - |
testcase_52 | AC | 15 ms
6,940 KB |
testcase_53 | AC | 36 ms
6,944 KB |
testcase_54 | AC | 37 ms
6,948 KB |
testcase_55 | AC | 39 ms
6,944 KB |
testcase_56 | AC | 36 ms
6,940 KB |
testcase_57 | AC | 37 ms
6,944 KB |
testcase_58 | AC | 36 ms
6,940 KB |
testcase_59 | AC | 38 ms
6,944 KB |
testcase_60 | AC | 41 ms
6,940 KB |
testcase_61 | AC | 34 ms
6,940 KB |
testcase_62 | AC | 40 ms
6,944 KB |
testcase_63 | AC | 32 ms
6,940 KB |
testcase_64 | AC | 42 ms
6,940 KB |
testcase_65 | AC | 46 ms
6,940 KB |
testcase_66 | AC | 46 ms
6,940 KB |
testcase_67 | AC | 47 ms
6,944 KB |
testcase_68 | AC | 42 ms
6,940 KB |
testcase_69 | AC | 44 ms
6,940 KB |
testcase_70 | AC | 44 ms
6,940 KB |
testcase_71 | AC | 42 ms
6,944 KB |
testcase_72 | AC | 44 ms
6,944 KB |
testcase_73 | AC | 52 ms
6,944 KB |
testcase_74 | AC | 50 ms
6,940 KB |
testcase_75 | WA | - |
testcase_76 | WA | - |
testcase_77 | WA | - |
testcase_78 | WA | - |
testcase_79 | WA | - |
testcase_80 | WA | - |
testcase_81 | AC | 21 ms
6,944 KB |
ソースコード
// 整数型で解けるという考察を飛ばして long double の範囲で解く 想定:数値誤差でWA #include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <numeric> #include <queue> #include <utility> #include <vector> // MaxFlow (Dinic algorithm) template <typename T> struct MaxFlow { struct edge { int to; T cap; int rev; }; std::vector<std::vector<edge>> edges; std::vector<int> level; // level[i] = distance between vertex S and i (Default: -1) std::vector<int> iter; // iteration counter, used for Dinic's DFS void bfs(int s) { level.assign(edges.size(), -1); std::queue<int> q; level[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (edge &e : edges[v]) { if (e.cap > 0 and level[e.to] < 0) { level[e.to] = level[v] + 1; q.push(e.to); } } } } T dfs_dinic(int v, int goal, T f) { if (v == goal) return f; for (int &i = iter[v]; i < (int)edges[v].size(); i++) { edge &e = edges[v][i]; if (e.cap > 0 and level[v] < level[e.to]) { T d = dfs_dinic(e.to, goal, std::min(f, e.cap)); if (d > 0) { e.cap -= d; edges[e.to][e.rev].cap += d; return d; } } } return 0; } MaxFlow(int N) { edges.resize(N); } void add_edge(int from, int to, T capacity) { edges[from].push_back(edge{to, capacity, (int)edges[to].size()}); edges[to].push_back(edge{from, (T)0, (int)edges[from].size() - 1}); } // Dinic algorithm // Complexity: O(V^2 E) T Dinic(int s, int t, T req) { T flow = 0; while (req > 0) { bfs(s); if (level[t] < 0) break; iter.assign(edges.size(), 0); T f; while ((f = dfs_dinic(s, t, req)) > 0) flow += f, req -= f; } return flow; } }; // 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); } Tvalue error = std::accumulate(x.begin(), x.end(), Tvalue(0)) - xsum; if (error > EPS or -error > EPS) { infeasible = true; } else { std::vector<Tvalue> xcs(N); for (int i = 0; i < N; i++) xcs[i] = x[i] * c[i]; std::sort(xcs.begin(), xcs.end(), [&](const Tvalue &l, const Tvalue &r) { return std::max(l, -l) < std::max(r, -r); }); fun = 0; for (const auto &v : xcs) fun += v; } } 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-8; using std::cin, std::cout, std::vector; int main() { using Num = long double; int N, M; Num 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]--, C[i] *= K; } auto maximize_xi = [&](int ie, const vector<Num> &xnow) -> Num { MaxFlow<Num> mf(N + 2); mf.add_edge(N, A[ie], 1LL << 60); mf.add_edge(N, B[ie], 1LL << 60); 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); Num ret = mf.Dinic(N, N + 1, 1LL << 62) / 2 - 1 - std::accumulate(xnow.begin(), xnow.end(), (Num)0); return std::min<Num>(ret, D[ie] / K); }; LinearProgrammingOnBasePolyhedron<Num> solver(C, maximize_xi, N - 1, 0, true); if (solver.infeasible) { cout << "-1\n"; } else { cout << llround(solver.fun) << '\n'; } }