結果
問題 | No.957 植林 |
ユーザー | ei1333333 |
提出日時 | 2019-12-03 03:30:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,554 ms / 2,000 ms |
コード長 | 5,872 bytes |
コンパイル時間 | 2,067 ms |
コンパイル使用メモリ | 220,168 KB |
実行使用メモリ | 35,052 KB |
最終ジャッジ日時 | 2024-07-07 01:43:27 |
合計ジャッジ時間 | 31,835 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 76 ms
29,028 KB |
testcase_04 | AC | 95 ms
27,028 KB |
testcase_05 | AC | 73 ms
30,776 KB |
testcase_06 | AC | 144 ms
32,096 KB |
testcase_07 | AC | 72 ms
27,796 KB |
testcase_08 | AC | 53 ms
29,252 KB |
testcase_09 | AC | 52 ms
29,956 KB |
testcase_10 | AC | 58 ms
30,888 KB |
testcase_11 | AC | 57 ms
30,432 KB |
testcase_12 | AC | 55 ms
30,416 KB |
testcase_13 | AC | 48 ms
28,312 KB |
testcase_14 | AC | 58 ms
32,868 KB |
testcase_15 | AC | 57 ms
29,244 KB |
testcase_16 | AC | 51 ms
28,572 KB |
testcase_17 | AC | 50 ms
27,956 KB |
testcase_18 | AC | 1,013 ms
28,976 KB |
testcase_19 | AC | 1,098 ms
29,936 KB |
testcase_20 | AC | 739 ms
30,608 KB |
testcase_21 | AC | 1,127 ms
31,320 KB |
testcase_22 | AC | 1,290 ms
32,008 KB |
testcase_23 | AC | 1,372 ms
32,544 KB |
testcase_24 | AC | 1,151 ms
33,472 KB |
testcase_25 | AC | 1,465 ms
33,976 KB |
testcase_26 | AC | 1,060 ms
33,984 KB |
testcase_27 | AC | 1,548 ms
34,048 KB |
testcase_28 | AC | 1,299 ms
34,248 KB |
testcase_29 | AC | 1,129 ms
33,924 KB |
testcase_30 | AC | 1,000 ms
33,996 KB |
testcase_31 | AC | 1,013 ms
29,584 KB |
testcase_32 | AC | 1,146 ms
29,812 KB |
testcase_33 | AC | 764 ms
30,972 KB |
testcase_34 | AC | 1,091 ms
31,116 KB |
testcase_35 | AC | 1,273 ms
33,252 KB |
testcase_36 | AC | 1,397 ms
32,392 KB |
testcase_37 | AC | 1,155 ms
33,844 KB |
testcase_38 | AC | 1,487 ms
33,908 KB |
testcase_39 | AC | 1,074 ms
34,088 KB |
testcase_40 | AC | 1,554 ms
35,052 KB |
testcase_41 | AC | 46 ms
33,608 KB |
testcase_42 | AC | 48 ms
34,092 KB |
testcase_43 | AC | 65 ms
33,908 KB |
testcase_44 | AC | 81 ms
34,036 KB |
testcase_45 | AC | 2 ms
6,944 KB |
testcase_46 | AC | 1 ms
6,940 KB |
testcase_47 | AC | 2 ms
6,944 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using int64 = long long; 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 * 4 >= V) { level = global_relabel(t); relabels = 0; } } return ex[t]; } }; int main() { int H, W; cin >> H >> W; vector< vector< int64 > > G(H, vector< int64 >(W)); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) cin >> G[i][j]; } vector< int64 > R(H), C(W); for(int i = 0; i < H; i++) cin >> R[i]; for(int i = 0; i < W; i++) cin >> C[i]; int64 sum = accumulate(begin(R), end(R), 0LL) + accumulate(begin(C), end(C), 0LL); PushRelabel< int64 > flow(H * W + H + W + 2); int S = H * W + H + W; int T = S + 1; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { flow.add_edge(S, i * W + j, G[i][j]); flow.add_edge(i * W + j, H * W + i, 1<<30); flow.add_edge(i * W + j, H * W + H + j, 1<<30); } } for(int i = 0; i < H; i++) { flow.add_edge(H * W + i, T, R[i]); } for(int i = 0; i < W; i++) { flow.add_edge(H * W + H + i, T, C[i]); } cout << sum - flow.max_flow(S, T) << endl; }