結果

問題 No.1284 Flip Game
コンテスト
ユーザー siman
提出日時 2023-07-13 05:50:20
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,729 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,068 ms
コンパイル使用メモリ 149,628 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2026-04-04 14:04:22
合計ジャッジ時間 6,463 ms
ジャッジサーバーID
(参考情報)
judge5_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:19:11: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   19 |   ll C[N][N];
      |           ^
main.cpp:19:11: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:17:7: note: declared here
   17 |   int N;
      |       ^
main.cpp:19:8: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   19 |   ll C[N][N];
      |        ^
main.cpp:19:8: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:17:7: note: declared here
   17 |   int N;
      |       ^
main.cpp:28:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   28 |   ll dp[L][2 * N];
      |            ^~~~~
main.cpp:28:16: note: read of non-const variable 'N' is not allowed in a constant expression
   28 |   ll dp[L][2 * N];
      |                ^
main.cpp:17:7: note: declared here
   17 |   int N;
      |       ^
main.cpp:28:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   28 |   ll dp[L][2 * N];
      |         ^
main.cpp:28:9: note: read of non-const variable 'L' is not allowed in a constant expression
main.cpp:27:7: note: declared here
   27 |   int L = (1 << (2 * N));
      |       ^
4 warnings generated.

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  ll C[N][N];

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> C[i][j];
    }
  }

  int L = (1 << (2 * N));
  ll dp[L][2 * N];
  memset(dp, -1, sizeof(dp));

  for (int i = 0; i < N; ++i) {
    int nmask = 1 << i;
    dp[nmask][i] = 0;
  }

  for (int mask = 0; mask < L; ++mask) {
    for (int i = 0; i < N; ++i) {
      if (dp[mask][i] == -1) continue;

      for (int j = 0; j < N; ++j) {
        if (i == j) continue;
        if (mask >> j & 1) continue;

        int nmask = mask | (1 << j);
        ll ncost = dp[mask][i] + C[i][j];

        if (dp[nmask][j] == -1 || dp[nmask][j] > ncost) {
          dp[nmask][j] = ncost;
        }
      }

      for (int j = 0; j < N; ++j) {
        if (i == j) continue;
        if ((mask >> j & 1) == 0) continue;
        if (mask >> (j + N) & 1) continue;

        int nmask = mask | (1 << (j + N));
        ll ncost = dp[mask][i] + C[i][j];

        if (dp[nmask][j] == -1 || dp[nmask][j] > ncost) {
          dp[nmask][j] = ncost;
        }
      }
    }
  }

  ll ans = LLONG_MAX;

  for (int mask = 0; mask < L; ++mask) {
    int lmask = mask >> N;
    if (__builtin_popcount(lmask) != N - 1) continue;

    for (int i = 0; i < N; ++i) {
      if (dp[mask][i] == -1) continue;
      if (lmask >> i & 1) continue;

      ans = min(ans, dp[mask][i]);
    }
  }

  for (int i = 0; i < N; ++i) {
    ans = min(ans, dp[L - 1][i]);
  }

  cout << ans << endl;

  return 0;
}
0