結果
問題 | No.957 植林 |
ユーザー | Pachicobue |
提出日時 | 2019-12-20 04:12:59 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 12,115 bytes |
コンパイル時間 | 2,851 ms |
コンパイル使用メモリ | 224,996 KB |
実行使用メモリ | 35,224 KB |
最終ジャッジ日時 | 2024-07-07 05:54:42 |
合計ジャッジ時間 | 45,575 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 136 ms
28,636 KB |
testcase_04 | AC | 115 ms
26,700 KB |
testcase_05 | AC | 173 ms
29,616 KB |
testcase_06 | AC | 104 ms
31,028 KB |
testcase_07 | AC | 135 ms
27,696 KB |
testcase_08 | AC | 58 ms
19,240 KB |
testcase_09 | AC | 58 ms
19,156 KB |
testcase_10 | AC | 56 ms
20,016 KB |
testcase_11 | AC | 59 ms
20,180 KB |
testcase_12 | AC | 60 ms
20,184 KB |
testcase_13 | AC | 32 ms
14,236 KB |
testcase_14 | AC | 47 ms
17,752 KB |
testcase_15 | AC | 39 ms
16,020 KB |
testcase_16 | AC | 33 ms
14,876 KB |
testcase_17 | AC | 35 ms
14,996 KB |
testcase_18 | AC | 1,127 ms
28,732 KB |
testcase_19 | AC | 1,510 ms
29,504 KB |
testcase_20 | AC | 1,230 ms
30,360 KB |
testcase_21 | AC | 1,805 ms
30,912 KB |
testcase_22 | AC | 1,231 ms
31,612 KB |
testcase_23 | AC | 1,925 ms
32,308 KB |
testcase_24 | AC | 1,606 ms
33,272 KB |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | AC | 1,094 ms
28,860 KB |
testcase_32 | AC | 1,549 ms
29,504 KB |
testcase_33 | AC | 1,201 ms
30,344 KB |
testcase_34 | AC | 1,747 ms
30,788 KB |
testcase_35 | AC | 1,267 ms
31,596 KB |
testcase_36 | TLE | - |
testcase_37 | AC | 1,671 ms
33,272 KB |
testcase_38 | TLE | - |
testcase_39 | TLE | - |
testcase_40 | TLE | - |
testcase_41 | AC | 13 ms
9,600 KB |
testcase_42 | AC | 12 ms
9,600 KB |
testcase_43 | AC | 65 ms
33,892 KB |
testcase_44 | AC | 67 ms
33,940 KB |
testcase_45 | AC | 2 ms
5,376 KB |
testcase_46 | AC | 2 ms
5,376 KB |
testcase_47 | AC | 2 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> // created [2019/12/20] 03:56:38 // N^2頂点で間に合った...(魔法ですか?) #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T, usize n> using arr = T (&)[n]; template<typename T, usize n> using c_arr = const T (&)[n]; template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T msbp1(const T u) { return log2p1(u); } template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); } template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; } template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); } template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); } template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); } template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); } template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); } template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); } template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); } template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4; template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; auto mfp = [](auto&& f) { return [=](auto&&... args) { return f(f, std::forward<decltype(args)>(args)...); }; }; template<typename T> T in() { T v; return std::cin >> v, v; } template<typename T, typename Uint, usize n, usize i> T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) { return in<T>(); } template<typename T, typename Uint, usize n, usize i> auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type& szs) { const usize s = (usize)szs[i]; std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s); for (usize j = 0; j < s; j++) { ans[j] = in_v<T, Uint, n, i + 1>(szs); } return ans; } template<typename T, typename Uint, usize n> auto in_v(c_arr<Uint, n> szs) { return in_v<T, Uint, n, 0>(szs); } template<typename... Types> auto in_t() { return std::tuple<std::decay_t<Types>...>{in<Types>()...}; } struct io_init { io_init() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(20); } void clear() { std::cin.tie(), std::ios::sync_with_stdio(true); } } io_setting; template<typename T> int out(const T& v) { return std::cout << v, 0; } template<typename T> int out(const std::vector<T>& v) { for (usize i = 0; i < v.size(); i++) { if (i > 0) { std::cout << ' '; } out(v[i]); } return std::cout << "\n", 0; } template<typename T1, typename T2> int out(const std::pair<T1, T2>& v) { return out(v.first), std::cout << ' ', out(v.second), 0; } template<typename T, typename... Args> int out(const T& v, const Args... args) { return out(v), std::cout << ' ', out(args...), 0; } template<typename... Args> int outln(const Args... args) { return out(args...), std::cout << '\n', 0; } template<typename... Args> void outel(const Args... args) { return out(args...), std::cout << std::endl, 0; } # define SHOW(...) static_cast<void>(0) constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type, const T& v = T{}) { return v; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs, const T& v = T{}) { const usize s = (usize)szs[i]; return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(s, make_v<T, Uint, n, i + 1>(szs, v)); } template<typename T, typename Uint, usize n> auto make_v(c_arr<Uint, n> szs, const T& t = T{}) { return make_v<T, Uint, n, 0>(szs, t); } using namespace std; class Stack { private: const int N, H; vector<int> node; public: Stack(const int N, const int H) : N(N), H(H), node(N + H) { clear(); } inline bool empty(const int h) const { return node[N + h] == N + h; } inline int top(const int h) const { return node[N + h]; } inline void pop(const int h) { node[N + h] = node[node[N + h]]; } inline void push(const int h, const int u) { node[u] = node[N + h], node[N + h] = u; } inline void clear() { iota(node.begin() + N, node.end(), N); } }; class List { public: struct node { int prev, next; }; const int N, H; vector<node> dat; List(const int N, const int H) : N(N), H(H), dat(N + H) { clear(); } inline bool empty(const int h) const { return (dat[N + h].next == N + h); } inline bool more_one(const int h) const { return dat[N + h].prev != dat[N + h].next; } inline void insert(const int h, const int u) { dat[u].prev = dat[N + h].prev, dat[u].next = N + h; dat[dat[N + h].prev].next = u, dat[N + h].prev = u; } inline void erase(const int u) { dat[dat[u].prev].next = dat[u].next, dat[dat[u].next].prev = dat[u].prev; } inline void clear() { for (int i = N; i < N + H; ++i) dat[i].prev = dat[i].next = i; } }; template<typename flow_t> struct PushRelabel { struct edge { int to; flow_t cap; int rev; bool isrev; int idx; }; vector<vector<edge>> graph; vector<int> potential, cur_edge; vector<flow_t> ex; int V, height, relabels; List all_ver; Stack act_ver; PushRelabel(int V) : V(V), height(-1), relabels(0), ex(V, flow_t(0)), potential(V, 0), cur_edge(V), all_ver(V, V), act_ver(V, V), graph(V) {} void add_edge(int from, int to, flow_t cap, int idx = -1) { graph[from].emplace_back((edge){to, cap, (int)graph[to].size(), false, idx}); graph[to].emplace_back((edge){from, 0, (int)graph[from].size() - 1, true, idx}); } int calc_active(int t) { height = -1; for (int i = 0; i < V; i++) { if (potential[i] < V) { cur_edge[i] = 0; height = max(height, potential[i]); all_ver.insert(potential[i], i); if (ex[i] > 0 && i != t) act_ver.push(potential[i], i); } else { potential[i] = V + 1; } } return height; } void bfs(int t) { for (int i = 0; i < V; i++) { potential[i] = max(potential[i], V); } potential[t] = 0; queue<int> que; que.emplace(t); while (!que.empty()) { int p = que.front(); que.pop(); for (auto& e : graph[p]) { if (potential[e.to] == V && graph[e.to][e.rev].cap > 0) { potential[e.to] = potential[p] + 1; que.emplace(e.to); } } } } int init(int s, int t) { potential[s] = V + 1; bfs(t); for (auto& e : graph[s]) { if (potential[e.to] < V) { graph[e.to][e.rev].cap = e.cap; ex[s] -= e.cap; ex[e.to] += e.cap; } e.cap = 0; } return calc_active(t); } bool push(int u, int t, edge& e) { flow_t f = min(e.cap, ex[u]); int v = e.to; e.cap -= f, ex[u] -= f; graph[v][e.rev].cap += f, ex[v] += f; if (ex[v] == f && v != t) act_ver.push(potential[v], v); return ex[u] == 0; } int discharge(int u, int t) { for (int& i = cur_edge[u]; i < graph[u].size(); i++) { auto& e = graph[u][i]; if (potential[u] == potential[e.to] + 1 && e.cap > 0) { if (push(u, t, e)) return potential[u]; } } return relabel(u); } int global_relabel(int t) { bfs(t); all_ver.clear(), act_ver.clear(); return calc_active(t); } void gap_relabel(const int u) { for (int i = potential[u]; i <= height; ++i) { for (int id = all_ver.dat[V + i].next; id < V; id = all_ver.dat[id].next) { potential[id] = V + 1; } all_ver.dat[V + i].prev = all_ver.dat[V + i].next = V + i; } } int relabel(const int u) { ++relabels; int prv = potential[u], cur = V; for (int i = 0; i < (int)graph[u].size(); ++i) { const edge& e = graph[u][i]; if (cur > potential[e.to] + 1 && e.cap > 0) { cur_edge[u] = i; cur = potential[e.to] + 1; } } if (all_ver.more_one(prv)) { all_ver.erase(u); if ((potential[u] = cur) == V) return potential[u] = V + 1, prv; act_ver.push(cur, u); all_ver.insert(cur, u); height = max(height, cur); } else { gap_relabel(u); return height = prv - 1; } return cur; } flow_t max_flow(int s, int t) { int level = init(s, t); while (level >= 0) { if (act_ver.empty(level)) { --level; continue; } int u = act_ver.top(level); act_ver.pop(level); level = discharge(u, t); if (relabels * 2 >= V) { level = global_relabel(t); relabels = 0; } } return ex[t]; } }; int main() { const auto [H, W] = in_t<int, int>(); const auto G = in_v<ll>({H, W}); auto R = in_v<ll>({H}); auto C = in_v<ll>({W}); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { R[i] -= G[i][j]; C[j] -= G[i][j]; } } constexpr ll inf = TEN(9); PushRelabel<ll> f(H * W + H + W + 2); // Dinic<ll> f(H * W + H + W + 2); const int S = H * W + H + W; const int T = H * W + H + W + 1; ll ans = 0; for (int i = 0; i < H; i++) { if (R[i] < 0) { f.add_edge(i, T, -R[i]); } else { ans += R[i]; } } for (int j = 0; j < W; j++) { if (C[j] < 0) { f.add_edge(H + j, T, -C[j]); } else { ans += C[j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { const int v = i * W + j + H + W; if (R[i] < 0) { f.add_edge(v, i, inf); } if (C[j] < 0) { f.add_edge(v, H + j, inf); } ans += G[i][j]; if (R[i] < 0 or C[j] < 0) { f.add_edge(S, v, G[i][j]); } } } SHOW(ans); outln(ans - f.max_flow(S, T)); return 0; }