結果

問題 No.1615 Double Down
ユーザー msm1993msm1993
提出日時 2021-08-02 09:57:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 666 ms / 10,000 ms
コード長 4,655 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 99,976 KB
実行使用メモリ 9,792 KB
最終ジャッジ日時 2023-08-12 08:07:35
合計ジャッジ時間 21,241 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 375 ms
8,976 KB
testcase_01 AC 377 ms
9,008 KB
testcase_02 AC 420 ms
9,252 KB
testcase_03 AC 424 ms
9,356 KB
testcase_04 AC 409 ms
9,344 KB
testcase_05 AC 411 ms
9,352 KB
testcase_06 AC 511 ms
9,792 KB
testcase_07 AC 441 ms
9,672 KB
testcase_08 AC 491 ms
9,740 KB
testcase_09 AC 663 ms
9,280 KB
testcase_10 AC 623 ms
9,412 KB
testcase_11 AC 666 ms
9,300 KB
testcase_12 AC 368 ms
9,564 KB
testcase_13 AC 411 ms
9,544 KB
testcase_14 AC 430 ms
9,456 KB
testcase_15 AC 218 ms
9,776 KB
testcase_16 AC 282 ms
9,696 KB
testcase_17 AC 281 ms
9,692 KB
testcase_18 AC 603 ms
9,408 KB
testcase_19 AC 571 ms
9,312 KB
testcase_20 AC 608 ms
9,412 KB
testcase_21 AC 613 ms
9,252 KB
testcase_22 AC 598 ms
9,440 KB
testcase_23 AC 607 ms
9,300 KB
testcase_24 AC 662 ms
9,312 KB
testcase_25 AC 584 ms
9,280 KB
testcase_26 AC 614 ms
9,252 KB
testcase_27 AC 71 ms
8,364 KB
testcase_28 AC 71 ms
8,220 KB
testcase_29 AC 117 ms
8,508 KB
testcase_30 AC 138 ms
8,600 KB
testcase_31 AC 130 ms
8,448 KB
testcase_32 AC 134 ms
8,696 KB
testcase_33 AC 231 ms
8,648 KB
testcase_34 AC 173 ms
8,644 KB
testcase_35 AC 287 ms
8,652 KB
testcase_36 AC 7 ms
7,672 KB
testcase_37 AC 5 ms
7,696 KB
testcase_38 AC 9 ms
7,680 KB
testcase_39 AC 6 ms
7,676 KB
testcase_40 AC 6 ms
7,816 KB
testcase_41 AC 5 ms
7,708 KB
testcase_42 AC 13 ms
7,788 KB
testcase_43 AC 14 ms
7,844 KB
testcase_44 AC 11 ms
7,828 KB
testcase_45 AC 3 ms
7,692 KB
testcase_46 AC 2 ms
7,660 KB
testcase_47 AC 2 ms
7,644 KB
testcase_48 AC 3 ms
7,620 KB
testcase_49 AC 2 ms
7,620 KB
testcase_50 AC 2 ms
7,608 KB
testcase_51 AC 2 ms
7,852 KB
testcase_52 AC 2 ms
7,584 KB
testcase_53 AC 2 ms
7,576 KB
testcase_54 AC 2 ms
7,664 KB
testcase_55 AC 2 ms
7,608 KB
testcase_56 AC 3 ms
7,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


namespace MCF {

using Capa = int;
using Cost = Int;
constexpr int MAX_N = 100'010;
constexpr int MAX_M = 100'010;
constexpr int QUE_SIZE = 1 << (32 - __builtin_clz(MAX_N));
constexpr int BELLMAN_FORD_NUM_ITERS = 10;
constexpr int LOG_SCALING = 2;

int n, m, ptr[MAX_N], cur[MAX_N], next[MAX_M << 1], zu[MAX_M << 1];
bool on[MAX_N];
int que[QUE_SIZE], qb, qe;
Capa capa[MAX_M << 1], ex[MAX_N];
Cost cost[MAX_M << 1], pot[MAX_N], pot0[MAX_N];

void init(int n_) {
  n = n_; m = 0; memset(ptr, ~0, n * sizeof(int)); memset(ex, 0, n * sizeof(Capa));
}
void ae(int u, int v, Capa c, Cost d) {
  d *= (n + 1);
  next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = c; cost[m] = d; ++m;
  next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -d; ++m;
}

bool bellmanFord(Cost eps) {
  memcpy(pot0, pot, n * sizeof(Cost));
  for (int iter = 0; iter < BELLMAN_FORD_NUM_ITERS; ++iter) {
    bool upd = false;
    for (int i = 0; i < m; ++i) {
      if (capa[i] > 0) {
        const int u = zu[i ^ 1], v = zu[i];
        if (pot0[v] > pot0[u] + cost[i] + eps) {
          pot0[v] = pot0[u] + cost[i] + eps;
          upd = true;
        }
      }
    }
    if (!upd) {
      memcpy(pot, pot0, n * sizeof(Cost));
      return true;
    }
  }
  return false;
}

Cost solve() {
  Cost minCost = 0;
  for (int i = 0; i < m; i += 2) if (minCost > cost[i]) minCost = cost[i];
  Cost eps = 1;
  for (; eps < -minCost; eps <<= LOG_SCALING) {}
  memset(pot, 0, n * sizeof(Cost));
  for (; eps >>= LOG_SCALING; ) {
    if (bellmanFord(eps)) continue;
    for (int i = 0; i < m; i += 2) {
      const int u = zu[i ^ 1], v = zu[i];
      const Cost d = cost[i] + pot[u] - pot[v];
      if (capa[i] > 0 && d < 0) {
        Capa &c = capa[i]; ex[u] -= c; ex[v] += c; capa[i ^ 1] += c; c = 0;
      } else if (capa[i ^ 1] > 0 && -d < 0) {
        Capa &c = capa[i ^ 1]; ex[v] -= c; ex[u] += c; capa[i] += c; c = 0;
      }
    }
    memcpy(cur, ptr, n * sizeof(int));
    qb = qe = 0;
    for (int u = 0; u < n; ++u) if (ex[u] > 0) {
      que[qe] = u; ++qe &= QUE_SIZE - 1;
    }
    for (; qb != qe; ) {
      const int u = que[qb]; ++qb &= QUE_SIZE - 1;
      for (int &i = cur[u]; ~i; i = next[i]) {
        if (capa[i] > 0) {
          const int v = zu[i];
          if (cost[i] + pot[u] - pot[v] < 0) {
            const Capa c = min(ex[u], capa[i]);
            if (ex[v] <= 0 && ex[v] + c > 0) {
              que[qe] = v; ++qe &= QUE_SIZE - 1;
            }
            ex[u] -= c; ex[v] += c; capa[i] -= c; capa[i ^ 1] += c;
            if (ex[u] == 0) break;
          }
        }
      }
      if (ex[u] > 0) {
        bool relabeled = false;
        for (int i = ptr[u]; ~i; i = next[i]) {
          if (capa[i] > 0) {
            const Cost p = pot[zu[i]] - cost[i] - eps;
            if (!relabeled || pot[u] < p) {
              relabeled = true; pot[u] = p;
            }
          }
        }
        cur[u] = ptr[u]; que[qe] = u; ++qe &= QUE_SIZE - 1;
      }
    }
  }
  Cost totalCost = 0;
  for (int i = 0; i < m; i += 2) totalCost += cost[i] * capa[i ^ 1];
  return totalCost / (n + 1);
}

}  // namespace MCF



int M, N, K, L;
vector<int> X, Y, Z;

int main() {
  for (; ~scanf("%d%d%d%d", &M, &N, &K, &L); ) {
    X.resize(L);
    Y.resize(L);
    Z.resize(L);
    for (int i = 0; i < L; ++i) {
      scanf("%d%d%d", &X[i], &Y[i], &Z[i]);
      --X[i];
      --Y[i];
    }
    
    MCF::init(1 + M + N);
    for (int u = 0; u < M; ++u) {
      MCF::ae(0, 1 + u, 1, 0);
    }
    for (int v = 0; v < N; ++v) {
      MCF::ae(1 + M + v, 0, 1, 0);
    }
    for (int i = 0; i < L; ++i) {
      MCF::ae(1 + X[i], 1 + M + Y[i], 1, -(1LL << Z[i]));
    }
    const Int ans = -MCF::solve();
    printf("%lld\n", ans);
  }
  return 0;
}
0