Submission #66137078


Source Code Expand

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#include"atcoder/all"
using namespace atcoder;
typedef modint998244353 mi;
using namespace std;
#define all(a) a.begin(),a.end()
#define compress(a) sort(all(a));a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
constexpr ll mod=998244353;
constexpr ll inf=3e18;
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);
  }
};

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};
}

int main(){
	int h,w;
	cin>>h>>w;
	vector<vector<ll>>A(h,vector<ll>(w));
	ll sum=0;
	rep(i,h){
		rep(j,w){
			cin>>A[i][j];
			sum+=A[i][j];
		}
	}

	Matrix<ll> mat((h*w+1)/2+1);
	rep(i,h){
		rep(j,w){
			if(j+1<w){
				int v1=(i*w+j),v2=i*w+j+1;

				if(A[i][j]+A[i][j+1]<0){
					if((i+j)%2==0){
						mat[v1/2+1][v2/2+1]=A[i][j]+A[i][j+1];
					}
					else mat[v2/2+1][v1/2+1]=A[i][j]+A[i][j+1];
				}
			}
			if(i+1<h){
				int v1=(i*w+j),v2=i*w+j+w;
				if(A[i][j]+A[i+1][j]<0){
					if((i+j)%2==0){
						mat[v1/2+1][v2/2+1]=A[i][j]+A[i+1][j];
					}
					else mat[v2/2+1][v1/2+1]=A[i][j]+A[i+1][j];
				}
			}
		}
	}

	cout<<sum-hungarian(mat).first<<endl;
}

Submission Info

Submission Time
Task G - Domino Covering SUM
User Rho17
Language C++ 20 (gcc 12.2)
Score 600
Code Size 5157 Byte
Status AC
Exec Time 1201 ms
Memory 11608 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 74
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt, 01_random_69.txt, 01_random_70.txt, 01_random_71.txt, 01_random_72.txt, 01_random_73.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3572 KiB
00_sample_01.txt AC 1 ms 3552 KiB
00_sample_02.txt AC 1 ms 3536 KiB
01_random_03.txt AC 602 ms 11280 KiB
01_random_04.txt AC 3 ms 3664 KiB
01_random_05.txt AC 8 ms 3836 KiB
01_random_06.txt AC 232 ms 7296 KiB
01_random_07.txt AC 429 ms 9764 KiB
01_random_08.txt AC 562 ms 10724 KiB
01_random_09.txt AC 613 ms 10928 KiB
01_random_10.txt AC 549 ms 10300 KiB
01_random_11.txt AC 629 ms 10956 KiB
01_random_12.txt AC 644 ms 11324 KiB
01_random_13.txt AC 715 ms 11492 KiB
01_random_14.txt AC 638 ms 11376 KiB
01_random_15.txt AC 1132 ms 11376 KiB
01_random_16.txt AC 1112 ms 11332 KiB
01_random_17.txt AC 1003 ms 10924 KiB
01_random_18.txt AC 1079 ms 11084 KiB
01_random_19.txt AC 74 ms 11244 KiB
01_random_20.txt AC 61 ms 11392 KiB
01_random_21.txt AC 19 ms 11216 KiB
01_random_22.txt AC 24 ms 11316 KiB
01_random_23.txt AC 1031 ms 11132 KiB
01_random_24.txt AC 1129 ms 11608 KiB
01_random_25.txt AC 1011 ms 11056 KiB
01_random_26.txt AC 980 ms 10880 KiB
01_random_27.txt AC 998 ms 10788 KiB
01_random_28.txt AC 89 ms 10848 KiB
01_random_29.txt AC 81 ms 10792 KiB
01_random_30.txt AC 114 ms 10828 KiB
01_random_31.txt AC 87 ms 11176 KiB
01_random_32.txt AC 1 ms 3572 KiB
01_random_33.txt AC 1 ms 3528 KiB
01_random_34.txt AC 1 ms 3528 KiB
01_random_35.txt AC 1 ms 3568 KiB
01_random_36.txt AC 1 ms 3520 KiB
01_random_37.txt AC 1 ms 3528 KiB
01_random_38.txt AC 1 ms 3568 KiB
01_random_39.txt AC 1 ms 3640 KiB
01_random_40.txt AC 589 ms 8604 KiB
01_random_41.txt AC 619 ms 8784 KiB
01_random_42.txt AC 647 ms 11000 KiB
01_random_43.txt AC 616 ms 10952 KiB
01_random_44.txt AC 617 ms 11100 KiB
01_random_45.txt AC 725 ms 11392 KiB
01_random_46.txt AC 561 ms 10816 KiB
01_random_47.txt AC 687 ms 11052 KiB
01_random_48.txt AC 667 ms 10804 KiB
01_random_49.txt AC 625 ms 10752 KiB
01_random_50.txt AC 576 ms 10916 KiB
01_random_51.txt AC 609 ms 11196 KiB
01_random_52.txt AC 581 ms 10344 KiB
01_random_53.txt AC 544 ms 10844 KiB
01_random_54.txt AC 610 ms 11036 KiB
01_random_55.txt AC 630 ms 10972 KiB
01_random_56.txt AC 605 ms 10876 KiB
01_random_57.txt AC 524 ms 10752 KiB
01_random_58.txt AC 639 ms 11376 KiB
01_random_59.txt AC 712 ms 10984 KiB
01_random_60.txt AC 588 ms 10952 KiB
01_random_61.txt AC 714 ms 10600 KiB
01_random_62.txt AC 1 ms 3528 KiB
01_random_63.txt AC 1 ms 3432 KiB
01_random_64.txt AC 1068 ms 10312 KiB
01_random_65.txt AC 1189 ms 10864 KiB
01_random_66.txt AC 1060 ms 10528 KiB
01_random_67.txt AC 1107 ms 11084 KiB
01_random_68.txt AC 1126 ms 11184 KiB
01_random_69.txt AC 1129 ms 11172 KiB
01_random_70.txt AC 1201 ms 11516 KiB
01_random_71.txt AC 1149 ms 11048 KiB
01_random_72.txt AC 1154 ms 11468 KiB
01_random_73.txt AC 1007 ms 10520 KiB