結果
問題 | No.1324 Approximate the Matrix |
ユーザー | 👑 emthrm |
提出日時 | 2020-12-25 23:30:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 79 ms / 2,000 ms |
コード長 | 5,180 bytes |
コンパイル時間 | 2,463 ms |
コンパイル使用メモリ | 216,268 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-22 17:03:14 |
合計ジャッジ時間 | 5,471 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | AC | 46 ms
5,376 KB |
testcase_04 | AC | 42 ms
5,376 KB |
testcase_05 | AC | 47 ms
5,376 KB |
testcase_06 | AC | 45 ms
5,376 KB |
testcase_07 | AC | 45 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 5 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 12 ms
5,376 KB |
testcase_12 | AC | 5 ms
5,376 KB |
testcase_13 | AC | 3 ms
5,376 KB |
testcase_14 | AC | 13 ms
5,376 KB |
testcase_15 | AC | 5 ms
6,944 KB |
testcase_16 | AC | 3 ms
6,940 KB |
testcase_17 | AC | 12 ms
6,944 KB |
testcase_18 | AC | 4 ms
6,940 KB |
testcase_19 | AC | 5 ms
6,940 KB |
testcase_20 | AC | 3 ms
6,944 KB |
testcase_21 | AC | 3 ms
6,944 KB |
testcase_22 | AC | 4 ms
6,940 KB |
testcase_23 | AC | 11 ms
6,944 KB |
testcase_24 | AC | 17 ms
6,940 KB |
testcase_25 | AC | 13 ms
6,940 KB |
testcase_26 | AC | 11 ms
6,940 KB |
testcase_27 | AC | 7 ms
6,940 KB |
testcase_28 | AC | 2 ms
6,940 KB |
testcase_29 | AC | 2 ms
6,944 KB |
testcase_30 | AC | 2 ms
6,940 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 2 ms
6,940 KB |
testcase_34 | AC | 2 ms
6,944 KB |
testcase_35 | AC | 1 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 78 ms
6,944 KB |
testcase_38 | AC | 78 ms
6,940 KB |
testcase_39 | AC | 78 ms
6,940 KB |
testcase_40 | AC | 78 ms
6,940 KB |
testcase_41 | AC | 79 ms
6,944 KB |
testcase_42 | AC | 5 ms
6,944 KB |
testcase_43 | AC | 5 ms
6,944 KB |
testcase_44 | AC | 5 ms
6,940 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> 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() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <typename T, typename U> struct PrimalDual { 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) {} }; std::vector<std::vector<Edge>> graph; PrimalDual(int n, const T TINF, const 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); } } std::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: using Pui = std::pair<U, int>; int n; const T TINF; const U UINF; bool has_negative_edge = false; std::vector<int> prev_v, prev_e; std::vector<U> potential, dist; std::priority_queue<Pui, std::vector<Pui>, std::greater<Pui>> que; void bellman_ford(int s) { std::fill(dist.begin(), dist.end(), UINF); dist[s] = 0; bool is_updated = true; for (int step = 0; step < n; ++step) { is_updated = false; for (int i = 0; i < n; ++i) { if (dist[i] == UINF) continue; for (int j = 0; j < graph[i].size(); ++j) { 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); for (int i = 0; i < n; ++i) { if (dist[i] != UINF) potential[i] += dist[i]; } } void dijkstra(int s) { std::fill(dist.begin(), dist.end(), 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; for (int i = 0; i < graph[ver].size(); ++i) { 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); } } } for (int i = 0; i < n; ++i) { 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 = std::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 n, k; cin >> n >> k; PrimalDual<int, int> pd(n * 2 + 2, INF, INF); const int s = n * 2, t = n * 2 + 1; vector<int> a(n), b(n); REP(i, n) { cin >> a[i]; pd.add_edge(s, i, a[i], 0); } REP(i, n) { cin >> b[i]; pd.add_edge(n + i, t, b[i], 0); } ll ans = 0; REP(i, n) REP(j, n) { int p; cin >> p; auto f = [&](int x) { return (x - p) * (x - p); }; ans += f(0); for (int x = 1; x <= a[i] && x <= b[j]; ++x) pd.add_edge(i, n + j, 1, f(x) - f(x - 1)); } cout << ans + pd.minimum_cost_flow(s, t, k) << '\n'; return 0; }