結果
問題 | No.957 植林 |
ユーザー | ei1333333 |
提出日時 | 2019-12-18 00:54:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 245 ms / 2,000 ms |
コード長 | 6,352 bytes |
コンパイル時間 | 2,404 ms |
コンパイル使用メモリ | 218,744 KB |
実行使用メモリ | 12,800 KB |
最終ジャッジ日時 | 2024-07-07 01:41:41 |
合計ジャッジ時間 | 8,875 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 22 ms
11,392 KB |
testcase_04 | AC | 21 ms
10,624 KB |
testcase_05 | AC | 20 ms
11,392 KB |
testcase_06 | AC | 27 ms
11,776 KB |
testcase_07 | AC | 22 ms
11,008 KB |
testcase_08 | AC | 15 ms
11,648 KB |
testcase_09 | AC | 16 ms
11,392 KB |
testcase_10 | AC | 17 ms
11,776 KB |
testcase_11 | AC | 16 ms
11,392 KB |
testcase_12 | AC | 15 ms
11,264 KB |
testcase_13 | AC | 14 ms
9,600 KB |
testcase_14 | AC | 17 ms
12,032 KB |
testcase_15 | AC | 15 ms
11,264 KB |
testcase_16 | AC | 13 ms
9,600 KB |
testcase_17 | AC | 14 ms
11,008 KB |
testcase_18 | AC | 177 ms
11,136 KB |
testcase_19 | AC | 186 ms
11,520 KB |
testcase_20 | AC | 205 ms
11,648 KB |
testcase_21 | AC | 203 ms
11,904 KB |
testcase_22 | AC | 214 ms
12,032 KB |
testcase_23 | AC | 226 ms
12,160 KB |
testcase_24 | AC | 240 ms
12,416 KB |
testcase_25 | AC | 236 ms
12,672 KB |
testcase_26 | AC | 233 ms
12,672 KB |
testcase_27 | AC | 236 ms
12,672 KB |
testcase_28 | AC | 238 ms
12,672 KB |
testcase_29 | AC | 233 ms
12,672 KB |
testcase_30 | AC | 235 ms
12,800 KB |
testcase_31 | AC | 183 ms
11,264 KB |
testcase_32 | AC | 184 ms
11,520 KB |
testcase_33 | AC | 195 ms
11,648 KB |
testcase_34 | AC | 202 ms
11,904 KB |
testcase_35 | AC | 211 ms
12,160 KB |
testcase_36 | AC | 226 ms
12,160 KB |
testcase_37 | AC | 237 ms
12,416 KB |
testcase_38 | AC | 235 ms
12,544 KB |
testcase_39 | AC | 237 ms
12,672 KB |
testcase_40 | AC | 245 ms
12,672 KB |
testcase_41 | AC | 14 ms
12,672 KB |
testcase_42 | AC | 15 ms
12,672 KB |
testcase_43 | AC | 20 ms
12,672 KB |
testcase_44 | AC | 22 ms
12,800 KB |
testcase_45 | AC | 2 ms
6,944 KB |
testcase_46 | AC | 2 ms
6,944 KB |
testcase_47 | AC | 2 ms
6,944 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; // const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } #define rep(i, n) for(int i = 0; i < (n); ++i) #define REP(i, b, n) for(int i = (b); i < (n); ++i) #define let(v, x) __typeof(x) v = (x) #define foreach(i, v) for(let(i, (v).begin());i!=(v).end();i++) struct Wave {//{{{ typedef int64 Cap; static const Cap INF = 1LL << 60; struct E {//{{{ int src, dst; Cap cap, _cap; int rev; E(int src, int dst, Cap cap, int rev) : src(src), dst(dst), cap(cap), _cap(0), rev(rev) {} };//}}} int n; vector< vector< E > > g; Wave(int n) : n(n), g(n) {} inline void add_edge(int src, int dst, Cap cap) {//{{{ if(src == dst) return; g[src].push_back(E(src, dst, cap, g[dst].size())); g[dst].push_back(E(dst, src, 0, g[src].size() - 1)); }//}}} inline E &rev(const E &e) { return g[e.dst][e.rev]; } vector< int > level, iter; vector< int > blocked; vector< int > unbalance[2]; // { unblocked, blocked } vector< int > inS; vector< Cap > ex; inline void push(E &e) {//{{{ Cap f = min(e.cap, ex[e.src]); if(!inS[e.dst]) unbalance[blocked[e.dst]].push_back(e.dst); inS[e.dst] = true; e.cap -= f; rev(e).cap += f; ex[e.src] -= f; ex[e.dst] += f; }//}}} // dir = +1 ? s to t : t to s inline void discharge(const int &u, const int dir) {//{{{ for(int &i = iter[u]; i < g[u].size(); ++i) { E &e = g[u][i]; if(e.cap == 0 || level[e.src] + dir != level[e.dst]) continue; if(dir == +1 && blocked[e.dst]) continue; push(e); if(ex[u] == 0) return; } blocked[u] = true; if(!inS[u]) unbalance[1].push_back(u); inS[u] = true; iter[u] = 0; }//}}} Cap run(int s, int t) {//{{{ vector< int > q(n); vector< int > tmp; tmp.reserve(n); rep(i, 2) unbalance[i].reserve(n); rep(i, 2) unbalance[i].clear(); inS.assign(n, false); inS[s] = inS[t] = true; for(Cap flow = 0;;) { level.assign(n, -1); int ql = 0, qr = 0; level[q[qr++] = s] = 0; while(ql != qr && level[t] == -1) { const int &u = q[ql++]; foreach(e, g[u]) if(level[e->dst] == -1 && e->cap + e->_cap > 0) level[q[qr++] = e->dst] = level[u] + 1; } if(level[t] == -1) return flow; rep(i, qr) { if(level[q[i]] == level[t] && q[i] != t) { level[q[i]] = -1; continue; } foreach(e, g[q[i]]) { if(level[e->src] + 1 == level[e->dst]) { e->cap += e->_cap; e->_cap = 0; } else { e->_cap += e->cap; e->cap = 0; } } } iter.assign(n, 0); blocked.assign(n, false); ex.assign(n, 0); ex[s] = INF; discharge(s, +1); ex[s] = 0; while(!unbalance[0].empty() || !unbalance[1].empty()) { rep(b, 2) while(!unbalance[b].empty()) { tmp.clear(); int l = level[unbalance[b].back()]; while(!unbalance[b].empty() && level[unbalance[b].back()] == l) { int v = unbalance[b].back(); unbalance[b].pop_back(); inS[v] = false; tmp.push_back(v); } foreach(v, tmp) discharge(*v, b ? -1 : +1); } } flow += 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); vector< int64 > row_sum(H); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) row_sum[i] += G[i][j]; } int64 sum_cut = 0; vector< int64 > row_cap(H); for(int i = 0; i < H; i++) { int64 cut = min(R[i], row_sum[i]); row_cap[i] = row_sum[i] - cut; sum_cut += cut; } Wave flow(H + W + 2); int S = H + W; int T = S + 1; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { flow.add_edge(i, H + j, G[i][j]); } } for(int i = 0; i < H; i++) { flow.add_edge(S, i, row_cap[i]); } for(int i = 0; i < W; i++) { flow.add_edge(H + i, T, C[i]); } cout << sum - flow.run(S, T) - sum_cut << endl; }