結果

問題 No.2573 moving up
ユーザー ei1333333ei1333333
提出日時 2023-12-02 16:32:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 7,097 bytes
コンパイル時間 5,057 ms
コンパイル使用メモリ 273,608 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-27 01:27:57
合計ジャッジ時間 8,622 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 271 ms
5,376 KB
testcase_02 AC 271 ms
5,376 KB
testcase_03 AC 263 ms
5,376 KB
testcase_04 AC 258 ms
5,376 KB
testcase_05 AC 260 ms
5,376 KB
testcase_06 AC 261 ms
5,376 KB
testcase_07 AC 249 ms
5,376 KB
testcase_08 AC 33 ms
5,376 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 188 ms
5,376 KB
testcase_11 AC 45 ms
5,376 KB
testcase_12 AC 105 ms
5,376 KB
testcase_13 AC 202 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
testcase_18 AC 115 ms
5,376 KB
testcase_19 AC 169 ms
5,376 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 19 ms
5,376 KB
testcase_22 AC 17 ms
5,376 KB
testcase_23 AC 29 ms
5,376 KB
testcase_24 AC 75 ms
5,376 KB
testcase_25 AC 122 ms
5,376 KB
testcase_26 AC 20 ms
5,376 KB
testcase_27 AC 7 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 6 ms
5,376 KB
testcase_30 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;

using int64 = long long;
constexpr int mod = 998244353;
constexpr int64 infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} 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 {
  explicit 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)};
}

#line 1 "math/matrix/matrix.hpp"
template< class T >
struct Matrix {
  vector< vector< T > > A;

  Matrix() {}

  Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}

  Matrix(size_t n) : A(n, vector< T >(n, 0)) {};

  size_t size() const {
    if(A.empty()) return 0;
    assert(A.size() == A[0].size());
    return A.size();
  }

  size_t height() const {
    return (A.size());
  }

  size_t width() const {
    return (A[0].size());
  }

  inline const vector< T > &operator[](int k) const {
    return (A.at(k));
  }

  inline vector< T > &operator[](int k) {
    return (A.at(k));
  }

  static Matrix I(size_t n) {
    Matrix mat(n);
    for(int i = 0; i < n; i++) mat[i][i] = 1;
    return (mat);
  }

  Matrix &operator+=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] += B[i][j];
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] -= B[i][j];
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    size_t n = height(), m = B.width(), p = width();
    assert(p == B.height());
    vector< vector< T > > C(n, vector< T >(m, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        for(int k = 0; k < p; k++)
          C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(height());
    while(k > 0) {
      if(k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

  Matrix operator+(const Matrix &B) const {
    return (Matrix(*this) += B);
  }

  Matrix operator-(const Matrix &B) const {
    return (Matrix(*this) -= B);
  }

  Matrix operator*(const Matrix &B) const {
    return (Matrix(*this) *= B);
  }

  Matrix operator^(const long long k) const {
    return (Matrix(*this) ^= k);
  }

  friend ostream &operator<<(ostream &os, Matrix &p) {
    size_t n = p.height(), m = p.width();
    for(int i = 0; i < n; i++) {
      os << "[";
      for(int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }


  T determinant() {
    Matrix B(*this);
    assert(width() == height());
    T ret = 1;
    for(int i = 0; i < width(); i++) {
      int idx = -1;
      for(int j = i; j < width(); j++) {
        if(B[j][i] != 0) idx = j;
      }
      if(idx == -1) return (0);
      if(i != idx) {
        ret *= -1;
        swap(B[i], B[idx]);
      }
      ret *= B[i][i];
      T vv = B[i][i];
      for(int j = 0; j < width(); j++) {
        B[i][j] /= vv;
      }
      for(int j = i + 1; j < width(); j++) {
        T a = B[j][i];
        for(int k = 0; k < width(); k++) {
          B[j][k] -= B[i][k] * a;
        }
      }
    }
    return (ret);
  }
};
#line 2 "graph/flow/hungarian.hpp"

/**
 * @brief Hungarian(二部グラフの最小重み最大マッチング)
 * @docs docs/hungarian.md
 */
template< typename T >
pair< T, vector< int > > hungarian(Matrix< T > &A) {
  const T infty = numeric_limits< T >::max();
  const int N = (int) A.height();
  const int M = (int) A.width();
  vector< int > P(M), way(M);
  vector< T > U(N, 0), V(M, 0), minV;
  vector< bool > used;

  for(int i = 1; i < N; i++) {
    P[0] = i;
    minV.assign(M, infty);
    used.assign(M, false);
    int j0 = 0;
    while(P[j0] != 0) {
      int i0 = P[j0], j1 = 0;
      used[j0] = true;
      T delta = infty;
      for(int j = 1; j < M; j++) {
        if(used[j]) continue;
        T curr = A[i0][j] - U[i0] - V[j];
        if(curr < minV[j]) minV[j] = curr, way[j] = j0;
        if(minV[j] < delta) delta = minV[j], j1 = j;
      }
      for(int j = 0; j < M; j++) {
        if(used[j]) U[P[j]] += delta, V[j] -= delta;
        else minV[j] -= delta;
      }
      j0 = j1;
    }
    do {
      P[j0] = P[way[j0]];
      j0 = way[j0];
    } while(j0 != 0);
  }
  return {-V[0], P};
}

const int vy[] = {-1, -1, 0, 0, 1, 1};
const int vx[] = {-1, 0, -1, 1, 0, 1};
int main() {
  int H, W;
  cin >> H >> W;
  vector< int > X(W), Y(W);
  for(int i = 0; i < W; i++) {
    cin >> X[i] >> Y[i];
  }
  Matrix< int > mat(W + 1, W + 1);
  for(int p = 0; p < W; p++) {
    vector<vector<int> > D(H);
    for (int i = 0; i < H; i++) {
      D[i].resize(i + W, inf);
    }
    queue<pair<int, int> > que;
    que.emplace(0, p);
    D[0][p] = 0;
    while (not que.empty()) {
      auto [y, x] = que.front();
      que.pop();
      for (int k = 0; k < 6; k++) {
        auto ny = y + vy[k];
        auto nx = x + vx[k];
        if (ny < 0 or ny >= H) continue;
        if (nx < 0 or nx >= D[ny].size()) continue;
        if (chmin(D[ny][nx], D[y][x] + 1)) {
          que.emplace(ny, nx);
        }
      }
    }
    for(int i = 0; i < W; i++) {
      mat[i + 1][p + 1] = D[X[i] - 1][Y[i] - 1];
    }
  }
  cout << hungarian(mat).first << endl;
}
0