結果
問題 | No.957 植林 |
ユーザー | niuez |
提出日時 | 2020-05-09 09:06:15 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,070 bytes |
コンパイル時間 | 1,899 ms |
コンパイル使用メモリ | 180,640 KB |
実行使用メモリ | 6,912 KB |
最終ジャッジ日時 | 2024-07-05 05:03:45 |
合計ジャッジ時間 | 14,589 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
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 | 10 ms
6,784 KB |
testcase_42 | AC | 10 ms
6,912 KB |
testcase_43 | AC | 16 ms
6,912 KB |
testcase_44 | WA | - |
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 <vector> #include <iostream> struct goldberg_tarjan { using cap_type = long long; struct edge { int to; cap_type cap; int rev; edge(int t, cap_type c, int r): to(t), cap(c), rev(r) {} }; int N; std::vector<std::vector<edge>> G; std::vector<cap_type> exc; std::vector<int> h; const cap_type INF = 1e18; goldberg_tarjan(int n): N(n), G(n) {} void add_edge(int from, int to, cap_type cap, cap_type rev_cap = 0) { G[from].emplace_back(to, cap, (int)(G[to].size())); G[to].emplace_back(from, rev_cap, (int)(G[from].size() - 1)); } void push(int from, edge& e) { cap_type fl = std::min(exc[from], e.cap); e.cap -= fl; G[e.to][e.rev].cap += fl; exc[from] -= fl; exc[e.to] += fl; } void relabel(int v) { h[v] = N - 1; for(const auto& e: G[v]) { if(e.cap > 0 && h[v] > h[e.to]) { h[v] = h[e.to]; } } h[v]++; } std::vector<int> Q; void global_relabeling(int t) { int qi = 0, qr = 0; Q[qr++] = t; h.assign(N, N - 1); h[t] = 0; while(qi < qr) { int v = Q[qi++]; for(const auto& e: G[v]) { if(G[e.to][e.rev].cap > 0 && h[v] + 1 < h[e.to]) { h[e.to] = h[v] + 1; Q[qr++] = e.to; } } } } cap_type max_flow(int s, int t) { exc.assign(N, 0); exc[s] = INF; h.assign(N, 0); Q.resize(N); int cnt = 0; global_relabeling(t); std::vector<std::vector<int>> que(N); std::vector<int> qi(N); que[h[s]].push_back(s); for(int di = h[s]; di >= 0;) { if(qi[di] == que[di].size()) { di--; continue; } int v = que[di][qi[di]++]; if(exc[v] == 0 || v == t) continue; for(auto& e: G[v]) { if(e.cap > 0 && h[v] == h[e.to] + 1) { push(v, e); if(exc[e.to] > 0 && e.to != t) { que[h[e.to]].push_back(e.to); } } } if(exc[v] == 0) continue; relabel(v); if(h[v] < N) { di = h[v]; que[h[v]].push_back(v); } if(++cnt % N == 0) { global_relabeling(t); } } return exc[t]; } }; #include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() template<class T> static inline std::vector<T> ndvec(size_t&& n, T val) noexcept { return std::vector<T>(n, std::forward<T>(val)); } template<class... Tail> static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept { return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...)); } template<class T, class Cond> struct chain { Cond cond; chain(Cond cond) : cond(cond) {} bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template<class T, class Cond> chain<T, Cond> make_chain(Cond cond) { return chain<T, Cond>(cond); } #include <vector> #include <queue> struct dinic { using cap_type = int; const cap_type INF = 1e9; struct edge { int to; cap_type cap; int rev; }; int n; std::vector<std::vector<edge>> g; dinic(int n): n(n), g(n) {} void add_edge(int from, int to, cap_type cap, cap_type rev_cap = 0) { g[from].push_back({ to, cap, (int)(g[to].size()) }); g[to].push_back({ from, rev_cap, (int)(g[from].size() - 1) }); } std::vector<int> level; std::vector<int> iter; cap_type dfs(const int s, const int v, cap_type mf) { if(s == v || mf == 0) return mf; for(int& i = iter[v]; i < g[v].size(); i++) { int t = g[v][i].to; edge& re = g[v][i]; edge& e = g[t][re.rev]; if(level[t] >= level[v] || e.cap == 0) continue; cap_type f = dfs(s, t, std::min(mf, e.cap)); if(f == 0) continue; e.cap -= f; re.cap += f; return f; } return 0; } cap_type max_flow(int s, int t) { std::vector<int> que(n); cap_type flow = 0; while(true) { level.assign(n, -1); int qi = 0; int qr = 0; level[s] = 0; que[qr++] = s; while(qi < qr && level[t]) { int v = que[qi++]; for(const auto& e: g[v]) { if(e.cap > 0 && level[e.to] == -1) { level[e.to] = level[v] + 1; que[qr++] = e.to; } } } if(level[t] == -1) break; iter.assign(n, 0); cap_type tmp; while((tmp = dfs(s, t, INF)) > 0) { flow += tmp; } } return flow; } }; int main() { cin.tie(nullptr); std::ios::sync_with_stdio(false); int H, W; cin >> H >> W; dinic gt(H + W + 2); int s = H + W; int t = s + 1; vector<i64> A(H, 0); rep(i,0,H) rep(j,0,W) { i64 g; cin >> g; A[i] += g; gt.add_edge(i, H + j, g); } i64 sum = 0; rep(i,0,H) { i64 r; cin >> r; i64 MIN = std::min(A[i], r); sum += r - MIN; gt.add_edge(s, i, A[i] - MIN); } rep(i,0,W) { i64 r; cin >> r; sum += r; gt.add_edge(H + i, t, r); } cout << sum - gt.max_flow(s, t) << "\n"; }