結果
問題 | No.957 植林 |
ユーザー | 👑 emthrm |
提出日時 | 2019-12-21 00:33:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 452 ms / 2,000 ms |
コード長 | 4,537 bytes |
コンパイル時間 | 1,836 ms |
コンパイル使用メモリ | 142,284 KB |
実行使用メモリ | 10,080 KB |
最終ジャッジ日時 | 2024-07-18 05:17:09 |
合計ジャッジ時間 | 12,478 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 19 ms
8,988 KB |
testcase_04 | AC | 19 ms
8,712 KB |
testcase_05 | AC | 18 ms
9,128 KB |
testcase_06 | AC | 23 ms
9,532 KB |
testcase_07 | AC | 19 ms
8,852 KB |
testcase_08 | AC | 15 ms
9,252 KB |
testcase_09 | AC | 14 ms
9,252 KB |
testcase_10 | AC | 16 ms
9,396 KB |
testcase_11 | AC | 15 ms
9,248 KB |
testcase_12 | AC | 16 ms
9,248 KB |
testcase_13 | AC | 13 ms
7,684 KB |
testcase_14 | AC | 16 ms
9,668 KB |
testcase_15 | AC | 14 ms
9,252 KB |
testcase_16 | AC | 13 ms
7,684 KB |
testcase_17 | AC | 14 ms
8,972 KB |
testcase_18 | AC | 326 ms
9,124 KB |
testcase_19 | AC | 348 ms
9,256 KB |
testcase_20 | AC | 347 ms
9,520 KB |
testcase_21 | AC | 373 ms
9,524 KB |
testcase_22 | AC | 397 ms
9,540 KB |
testcase_23 | AC | 413 ms
9,692 KB |
testcase_24 | AC | 411 ms
9,944 KB |
testcase_25 | AC | 450 ms
10,076 KB |
testcase_26 | AC | 446 ms
9,952 KB |
testcase_27 | AC | 443 ms
9,948 KB |
testcase_28 | AC | 446 ms
10,076 KB |
testcase_29 | AC | 449 ms
10,076 KB |
testcase_30 | AC | 452 ms
10,076 KB |
testcase_31 | AC | 324 ms
9,252 KB |
testcase_32 | AC | 343 ms
9,208 KB |
testcase_33 | AC | 355 ms
9,520 KB |
testcase_34 | AC | 378 ms
9,532 KB |
testcase_35 | AC | 396 ms
9,664 KB |
testcase_36 | AC | 411 ms
9,672 KB |
testcase_37 | AC | 419 ms
9,944 KB |
testcase_38 | AC | 447 ms
10,076 KB |
testcase_39 | AC | 446 ms
10,080 KB |
testcase_40 | AC | 446 ms
10,072 KB |
testcase_41 | AC | 12 ms
9,816 KB |
testcase_42 | AC | 12 ms
9,944 KB |
testcase_43 | AC | 18 ms
9,816 KB |
testcase_44 | AC | 18 ms
9,948 KB |
testcase_45 | AC | 1 ms
6,940 KB |
testcase_46 | AC | 2 ms
6,940 KB |
testcase_47 | AC | 2 ms
6,940 KB |
ソースコード
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #define _USE_MATH_DEFINES #include <cmath> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; // const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, // dx[] = {0, -1, -1, -1, 0, 1, 1, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; /*-------------------------------------------------*/ template <typename T> struct Dinic { struct Edge { int dst, rev; T cap; Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {} }; vector<vector<Edge> > graph; Dinic(int n) : graph(n), level(n), itr(n) {} void add_edge(int src, int dst, T cap) { graph[src].emplace_back(dst, cap, graph[dst].size()); graph[dst].emplace_back(src, 0, graph[src].size() - 1); } T maximum_flow(int s, int t, T limit) { T res = 0; while (true) { bfs(s); if (level[t] == -1) return res; fill(ALL(itr), 0); T tmp; while ((tmp = dfs(s, t, limit)) > 0) res += tmp; } } private: vector<int> level, itr; void bfs(int s) { fill(ALL(level), -1); queue<int> que; level[s] = 0; que.emplace(s); while (!que.empty()) { int ver = que.front(); que.pop(); for (Edge e : graph[ver]) if (level[e.dst] == -1 && e.cap > 0) { level[e.dst] = level[ver] + 1; que.emplace(e.dst); } } } T dfs(int ver, int t, T flow) { if (ver == t) return flow; for (; itr[ver] < graph[ver].size(); ++itr[ver]) { Edge &e = graph[ver][itr[ver]]; if (level[ver] < level[e.dst] && e.cap > 0) { T tmp = dfs(e.dst, t, min(flow, e.cap)); if (tmp > 0) { e.cap -= tmp; graph[e.dst][e.rev].cap += tmp; return tmp; } } } return 0; } }; template <template <typename> class C, typename T> struct ProjectSelectionProblem { ProjectSelectionProblem(int n, T TINF) : n(n), TINF(TINF) {} void add_diff(int u, int v, T cost) { assert(cost >= 0); diff_u.emplace_back(u); diff_v.emplace_back(v); diff_cost.emplace_back(cost); } void add_same(int u, int v, int group, T cost) { assert(cost <= 0); cost *= -1; res += cost; add(n, group ^ 1, cost); if (group == 0) { add_diff(n, u, TINF); add_diff(n, v, TINF); } else if (group == 1) { add_diff(u, n, TINF); add_diff(v, n, TINF); } ++n; } void add(int ver, int group, T cost) { if (cost < 0) { cost *= -1; res += cost; add(ver, group ^ 1, cost); } else { if (group == 0) { add_diff(ver, -1, cost); } else { add_diff(-2, ver, cost); } } } T solve() { C<T> flow(n + 2); int sz = diff_u.size(); REP(i, sz) { if (diff_u[i] < 0) diff_u[i] += n + 2; if (diff_v[i] < 0) diff_v[i] += n + 2; flow.add_edge(diff_u[i], diff_v[i], diff_cost[i]); } sz = same_u.size(); REP(i, sz) { if (same_u[i] < 0) same_u[i] += n + 2; if (same_v[i] < 0) same_v[i] += n + 2; flow.add_edge(same_u[i], same_v[i], same_cost[i]); } return flow.maximum_flow(n, n + 1, TINF) - res; } private: int n; T TINF, res = 0; vector<int> diff_u, diff_v, same_u, same_v; vector<T> diff_cost, same_cost; }; int main() { int h, w; cin >> h >> w; vector<vector<int> > g(h, vector<int>(w)); REP(i, h) REP(j, w) cin >> g[i][j]; vector<int> r(h), c(w); REP(i, h) cin >> r[i]; REP(i, w) cin >> c[i]; ProjectSelectionProblem<Dinic, long long> psp(h + w, LINF); REP(i, h) { long long cost = -r[i]; REP(j, w) cost += g[i][j]; psp.add(i, 1, cost); } REP(j, w) psp.add(h + j, 1, -c[j]); REP(i, h) REP(j, w) psp.add_diff(i, h + j, g[i][j]); cout << -psp.solve() << '\n'; return 0; }