結果
問題 | No.957 植林 |
ユーザー | hitonanode |
提出日時 | 2021-08-26 21:52:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 173 ms / 2,000 ms |
コード長 | 4,944 bytes |
コンパイル時間 | 1,410 ms |
コンパイル使用メモリ | 96,088 KB |
実行使用メモリ | 9,280 KB |
最終ジャッジ日時 | 2024-11-18 19:57:00 |
合計ジャッジ時間 | 6,140 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 43 ms
8,392 KB |
testcase_04 | AC | 66 ms
8,388 KB |
testcase_05 | AC | 33 ms
8,656 KB |
testcase_06 | AC | 173 ms
8,900 KB |
testcase_07 | AC | 39 ms
8,520 KB |
testcase_08 | AC | 20 ms
8,608 KB |
testcase_09 | AC | 20 ms
8,604 KB |
testcase_10 | AC | 22 ms
8,676 KB |
testcase_11 | AC | 20 ms
8,728 KB |
testcase_12 | AC | 21 ms
8,596 KB |
testcase_13 | AC | 15 ms
7,752 KB |
testcase_14 | AC | 20 ms
9,016 KB |
testcase_15 | AC | 18 ms
8,636 KB |
testcase_16 | AC | 17 ms
7,744 KB |
testcase_17 | AC | 18 ms
8,380 KB |
testcase_18 | AC | 87 ms
8,620 KB |
testcase_19 | AC | 89 ms
8,744 KB |
testcase_20 | AC | 90 ms
8,744 KB |
testcase_21 | AC | 95 ms
8,884 KB |
testcase_22 | AC | 96 ms
9,004 KB |
testcase_23 | AC | 105 ms
9,140 KB |
testcase_24 | AC | 111 ms
9,272 KB |
testcase_25 | AC | 113 ms
9,228 KB |
testcase_26 | AC | 114 ms
9,280 KB |
testcase_27 | AC | 108 ms
9,132 KB |
testcase_28 | AC | 108 ms
9,276 KB |
testcase_29 | AC | 109 ms
9,276 KB |
testcase_30 | AC | 108 ms
9,152 KB |
testcase_31 | AC | 85 ms
8,624 KB |
testcase_32 | AC | 89 ms
8,744 KB |
testcase_33 | AC | 93 ms
8,748 KB |
testcase_34 | AC | 93 ms
9,012 KB |
testcase_35 | AC | 97 ms
9,000 KB |
testcase_36 | AC | 98 ms
9,264 KB |
testcase_37 | AC | 104 ms
9,140 KB |
testcase_38 | AC | 108 ms
9,276 KB |
testcase_39 | AC | 110 ms
9,280 KB |
testcase_40 | AC | 111 ms
9,276 KB |
testcase_41 | AC | 13 ms
9,276 KB |
testcase_42 | AC | 13 ms
9,280 KB |
testcase_43 | AC | 20 ms
9,280 KB |
testcase_44 | AC | 21 ms
9,276 KB |
testcase_45 | AC | 2 ms
5,248 KB |
testcase_46 | AC | 1 ms
5,248 KB |
testcase_47 | AC | 2 ms
5,248 KB |
ソースコード
#line 1 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/957" #line 2 "combinatorial_opt/maxflow_pushrelabel.hpp" #include <cassert> #include <limits> #include <utility> #include <vector> // Maxflow (push-relabel, highest-label) // Complexity: O(N^2 M^(1/2)) template <class Cap, Cap INF = std::numeric_limits<Cap>::max() / 2, bool UseGlobalRelabeling = false> struct mf_pushrelabel { struct pque_ { std::vector<std::pair<int, int>> even_, odd_; pque_() : even_(0), odd_(0) {} void clear() { even_.clear(), odd_.clear(); } bool empty() const { return even_.empty() and odd_.empty(); } void push(int i, int h) { if (even_.size() and even_.back().second - 1 > h) throw; if (odd_.size() and odd_.back().second - 1 > h) throw; (h & 1 ? odd_ : even_).emplace_back(i, h); } int pop() { int ret; if (even_.empty() or (odd_.size() and odd_.back().second > even_.back().second)) { return ret = odd_.back().first, odd_.pop_back(), ret; } else { return ret = even_.back().first, even_.pop_back(), ret; } } } pque; int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::vector<_edge>> g; std::vector<std::pair<int, int>> pos; mf_pushrelabel(int n) : _n(n), g(n) { static_assert(INF > 0, "INF must be positive."); } int add_edge(int from, int to, Cap cap) { assert(0 <= from and from < _n); assert(0 <= to and to < _n); assert(0 <= cap); int m = int(pos.size()); pos.emplace_back(from, int(g[from].size())); int from_id = g[from].size(), to_id = g[to].size() + (from == to); g[from].push_back({to, to_id, cap}); g[to].push_back({from, from_id, Cap(0)}); return m; } std::vector<int> dist; std::vector<Cap> excess; void activate(int i) { pque.push(i, dist[i]); } void global_relabeling(int t) { if (!UseGlobalRelabeling) return; dist.assign(_n, _n), dist[t] = 0; static std::vector<int> q; q = {t}; int qh = 0; pque.clear(); while (qh < int(q.size())) { int now = q[qh++]; if (excess[now] > 0) activate(now); for (const auto &e : g[now]) { if (g[e.to][e.rev].cap and dist[e.to] == _n) { dist[e.to] = dist[now] + 1; q.push_back(e.to); } } } } Cap flow(const int &s, const int &t) { assert(0 <= s and s < _n); assert(0 <= t and t < _n); assert(s != t); excess.assign(_n, 0); excess[s] = INF, excess[t] = -INF; dist.assign(_n, 0); dist[s] = _n; pque.clear(); for (auto &e : g[s]) push(s, e); global_relabeling(t); int tick = _n; while (!pque.empty()) { int i = pque.pop(); int dnxt = _n * 2 - 1; for (auto &e : g[i]) { if (!e.cap) continue; if (dist[e.to] == dist[i] - 1) { push(i, e); if (excess[i] == 0) break; } else { if (dist[e.to] + 1 < dnxt) dnxt = dist[e.to] + 1; } } if (excess[i] > 0) { dist[i] = dnxt; activate(i); } if (--tick == 0) tick = _n, global_relabeling(t); } return excess[t] + INF; } void push(int i, _edge &e) { Cap delta = e.cap < excess[i] ? e.cap : excess[i]; excess[i] -= delta, e.cap -= delta; excess[e.to] += delta, g[e.to][e.rev].cap += delta; if (excess[e.to] > 0 and excess[e.to] <= delta) activate(e.to); } }; #line 3 "combinatorial_opt/test/maxflow.pushrelabel.yuki957.test.cpp" #include <algorithm> #include <iostream> #include <numeric> using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<int>> G(H, vector<int>(W)); for (auto &v : G) { for (auto &x : v) cin >> x; } vector<int> R(H), C(W); for (auto &x : R) cin >> x; for (auto &x : C) cin >> x; long long tot = accumulate(R.begin(), R.end(), 0LL) + accumulate(C.begin(), C.end(), 0LL); long long f1 = 0; int Z = 1 + H + W; mf_pushrelabel<long long> g(Z + 1); for (int i = 0; i < H; i++) { auto gtot = accumulate(G[i].begin(), G[i].end(), 0LL); auto f0 = min<long long>(gtot, R[i]); f1 += f0; g.add_edge(0, i + 1, gtot - f0); for (int j = 0; j < W; j++) g.add_edge(i + 1, H + 1 + j, G[i][j]); } for (int j = 0; j < W; j++) g.add_edge(H + 1 + j, Z, C[j]); cout << tot - f1 - g.flow(0, Z) << '\n'; }