結果
問題 | No.957 植林 |
ユーザー | 👑 emthrm |
提出日時 | 2019-12-20 01:35:05 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,265 bytes |
コンパイル時間 | 1,308 ms |
コンパイル使用メモリ | 140,752 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-07-07 02:22:52 |
合計ジャッジ時間 | 4,372 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 2 ms
6,816 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 | 12 ms
10,624 KB |
testcase_42 | AC | 188 ms
10,752 KB |
testcase_43 | AC | 18 ms
10,624 KB |
testcase_44 | AC | 16 ms
10,624 KB |
testcase_45 | AC | 2 ms
6,940 KB |
testcase_46 | AC | 2 ms
6,940 KB |
testcase_47 | WA | - |
ソースコード
#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, typename U> struct PrimalDual { using Pui = pair<U, int>; struct Edge { int dst, rev; T cap; U cost; Edge(int dst, T cap, U cost, int rev) : dst(dst), cap(cap), cost(cost), rev(rev) {} }; vector<vector<Edge> > graph; PrimalDual(int n, T TINF, U UINF) : n(n), TINF(TINF), UINF(UINF), graph(n), prev_v(n, -1), prev_e(n, -1), potential(n, 0), dist(n) {} void add_edge(int src, int dst, T cap, U cost) { has_negative_edge |= cost < 0; graph[src].emplace_back(dst, cap, cost, graph[dst].size()); graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1); } U minimum_cost_flow(int s, int t, T flow) { U res = 0; if (has_negative_edge) { bellman_ford(s); if (dist[t] == UINF) return UINF; res += calc(s, t, flow); } while (flow > 0) { dijkstra(s); if (dist[t] == UINF) return UINF; res += calc(s, t, flow); } return res; } U minimum_cost_flow(int s, int t) { U res = 0; bellman_ford(s); if (potential[t] >= 0 || dist[t] == UINF) return res; T tmp = TINF; res += calc(s, t, tmp); while (true) { dijkstra(s); if (potential[t] >= 0 || dist[t] == UINF) return res; res += calc(s, t, tmp); } } pair<T, U> min_cost_max_flow(int s, int t, T flow) { T mx = flow; U cost = 0; if (has_negative_edge) { bellman_ford(s); if (dist[t] == UINF) return {mx - flow, cost}; cost += calc(s, t, flow); } while (flow > 0) { dijkstra(s); if (dist[t] == UINF) return {mx - flow, cost}; cost += calc(s, t, flow); } return {mx - flow, cost}; } private: int n; T TINF; U UINF; bool has_negative_edge = false; vector<int> prev_v, prev_e; vector<U> potential, dist; priority_queue<Pui, vector<Pui>, greater<Pui> > que; void bellman_ford(int s) { fill(ALL(dist), UINF); dist[s] = 0; bool is_updated = true; REP(step, n) { is_updated = false; REP(i, n) if (dist[i] != UINF) { REP(j, graph[i].size()) { Edge e = graph[i][j]; if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) { dist[e.dst] = dist[i] + e.cost; prev_v[e.dst] = i; prev_e[e.dst] = j; is_updated = true; } } } if (!is_updated) break; } assert(!is_updated); REP(i, n) { if (dist[i] != UINF) potential[i] += dist[i]; } } void dijkstra(int s) { fill(ALL(dist), UINF); dist[s] = 0; que.emplace(0, s); while (!que.empty()) { Pui pr = que.top(); que.pop(); int ver = pr.second; if (dist[ver] < pr.first) continue; REP(i, graph[ver].size()) { Edge e = graph[ver][i]; U nx = dist[ver] + e.cost + potential[ver] - potential[e.dst]; if (e.cap > 0 && dist[e.dst] > nx) { dist[e.dst] = nx; prev_v[e.dst] = ver; prev_e[e.dst] = i; que.emplace(dist[e.dst], e.dst); } } } REP(i, n) { if (dist[i] != UINF) potential[i] += dist[i]; } } U calc(int s, int t, T &flow) { T f = flow; for (int v = t; v != s; v = prev_v[v]) f = min(f, graph[prev_v[v]][prev_e[v]].cap); flow -= f; for (int v = t; v != s; v = prev_v[v]) { Edge &e = graph[prev_v[v]][prev_e[v]]; e.cap -= f; graph[v][e.rev].cap += f; } return potential[t] * f; } }; 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]; PrimalDual<int, long long> pd(h + w + 2, INF, LINF); int s = h + w, t = h + w + 1; REP(i, h) { long long cost = -r[i]; REP(j, w) cost += g[i][j]; pd.add_edge(s, i, 1, cost); pd.add_edge(i, t, 1, 0); } REP(j, w) { pd.add_edge(s, h + j, 1, 0); long long cost = -c[j]; REP(i, h) cost += g[i][j]; pd.add_edge(h + j, t, 1, cost); } REP(i, h) REP(j, w) pd.add_edge(i, h + j, 1, -g[i][j]); cout << -pd.minimum_cost_flow(s, t) << '\n'; return 0; }