結果
問題 | No.957 植林 |
ユーザー | KoD |
提出日時 | 2020-07-17 17:51:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,467 bytes |
コンパイル時間 | 1,293 ms |
コンパイル使用メモリ | 100,496 KB |
実行使用メモリ | 10,496 KB |
最終ジャッジ日時 | 2024-05-06 18:45:01 |
合計ジャッジ時間 | 12,324 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | AC | 40 ms
8,832 KB |
testcase_05 | AC | 38 ms
9,600 KB |
testcase_06 | WA | - |
testcase_07 | AC | 36 ms
8,832 KB |
testcase_08 | AC | 32 ms
9,344 KB |
testcase_09 | AC | 32 ms
9,344 KB |
testcase_10 | AC | 34 ms
9,600 KB |
testcase_11 | AC | 31 ms
9,344 KB |
testcase_12 | WA | - |
testcase_13 | AC | 27 ms
7,552 KB |
testcase_14 | AC | 34 ms
9,728 KB |
testcase_15 | AC | 30 ms
9,472 KB |
testcase_16 | AC | 27 ms
7,808 KB |
testcase_17 | AC | 29 ms
8,704 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | AC | 21 ms
10,112 KB |
testcase_42 | AC | 21 ms
10,112 KB |
testcase_43 | AC | 45 ms
10,112 KB |
testcase_44 | AC | 49 ms
10,112 KB |
testcase_45 | AC | 2 ms
5,376 KB |
testcase_46 | AC | 1 ms
5,376 KB |
testcase_47 | AC | 2 ms
5,376 KB |
ソースコード
/** * @title Template */ #include <iostream> #include <algorithm> #include <utility> #include <numeric> #include <vector> #include <array> #include <queue> #include <list> template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } struct range { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { ++i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { } constexpr iterator begin() const noexcept { return l; } constexpr iterator end() const noexcept { return r; } }; struct revrange { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { --i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { } constexpr iterator begin() const noexcept { return r; } constexpr iterator end() const noexcept { return l; } }; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; constexpr i32 inf32 = (i32(1) << 30) - 1; constexpr i64 inf64 = (i64(1) << 62) - 1; struct edge_t { i32 to; i64 cap; i32 rev; }; int main() { i32 H, W; std::cin >> H >> W; i32 V = H + W + 2; i32 source = H + W, sink = H + W + 1; std::vector<std::vector<edge_t>> graph(V); auto add_edge = [&](i32 u, i32 v, i64 c) { graph[u].push_back(edge_t{ v, c, (i32) graph[v].size() }); graph[v].push_back(edge_t{ u, 0, (i32) graph[u].size() - 1 }); }; std::vector<i64> accum(H); for (auto i: range(0, H)) { for (auto j: range(0, W)) { i32 g; std::cin >> g; accum[i] += g; add_edge(i, H + j, g); } } i64 sum = 0; for (auto i: range(0, H)) { i64 r; std::cin >> r; i64 min = std::min(accum[i], r); sum += r - min; add_edge(source, i, accum[i] - min); } for (auto i: range(0, W)) { i64 r; std::cin >> r; sum += r; add_edge(H + i, sink, r); } i32 min_gap, max_active; std::vector<std::list<i32>> active(V), inactive(V); std::vector<i32> height(V), seen(V), count(V); std::vector<i64> excess(V); std::vector<typename std::list<i32>::iterator> iter(V); i32 counter = 0; auto push = [&](auto u, edge_t &e) { // Push flow from the node. i64 flow = std::min(e.cap, excess[u]); e.cap -= flow; graph[e.to][e.rev].cap += flow; excess[u] -= flow; excess[e.to] += flow; }; auto relabel = [&](auto u) { // Relabel the node so that there will be an admissible edge. ++counter; count[height[u]]--; if (count[height[u]] == 0) { for (auto i: range(height[u], min_gap)) { for (auto v: active[i]) { height[v] = V + 1; } for (auto v: inactive[i]) { height[v] = V + 1; } active[i].clear(); inactive[i].clear(); } min_gap = height[u]; height[u] = V + 1; chmin(max_active, min_gap - 1); return; } i32 min = V + 1; for (i32 i = 0; i < graph[u].size(); ++i) { if (graph[u][i].cap > 0 && chmin(min, height[graph[u][i].to] + 1)) { seen[u] = i; } } height[u] = min; if (height[u] > min_gap) { height[u] = V + 1; } else { if (height[u] == min_gap) { ++min_gap; } chmax(max_active, height[u]); iter[u] = active[height[u]].insert(active[height[u]].end(), u); ++count[height[u]]; } }; auto reverse_bfs = [&] { // Compute exact heights. std::fill(height.begin(), height.end(), V + 1); height[sink] = 0; std::queue<i32> que; que.push(sink); while (!que.empty()) { i32 u = que.front(); que.pop(); for (auto e: graph[u]) { if (graph[e.to][e.rev].cap > 0) { if (chmin(height[e.to], height[u] + 1)) { que.push(e.to); } } } } }; auto set_active = [&] { // Count nodes with each height and set active nodes. min_gap = V; max_active = 0; for (auto h: range(0, V)) { active[h].clear(); inactive[h].clear(); count[h] = 0; } for (auto u: range(0, V)) { if (height[u] < V) { count[height[u]]++; if (excess[u] > 0 && u != sink) { iter[u] = active[height[u]].insert(active[height[u]].end(), u); chmax(max_active, height[u]); } else { iter[u] = inactive[height[u]].insert(inactive[height[u]].end(), u); } } } for (auto h: range(0, V)) { if (count[h] == 0) { min_gap = h; break; } } }; auto discharge = [&](auto u) { // Apply push/relabel until the node becomes inactive. while (seen[u] < graph[u].size()) { auto &e = graph[u][seen[u]]; seen[u]++; if (e.cap > 0 && height[u] == height[e.to] + 1) { { if (excess[e.to] == 0 && e.to != sink) { inactive[height[e.to]].erase(iter[e.to]); iter[e.to] = active[height[e.to]].insert(active[height[e.to]].end(), e.to); chmax(max_active, height[e.to]); } } push(u, e); if (excess[u] == 0) { iter[u] = inactive[height[u]].insert(inactive[height[u]].end(), u); return; } } } relabel(u); }; { // Preprocess reverse_bfs(); if (height[source] == V + 1) { std::cout << sum << '\n'; return 0; } for (auto &e: graph[source]) { excess[source] += e.cap; push(source, e); } height[source] = V; set_active(); } { // Main Process while (max_active > 0) { if (active[max_active].empty()) { --max_active; continue; } auto itr = active[max_active].begin(); active[max_active].erase(itr); discharge(*itr); } } std::cout << sum - excess[sink] << '\n'; return 0; }