結果

問題 No.1284 Flip Game
ユーザー tonyu0tonyu0
提出日時 2020-11-08 10:59:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 989 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 79,840 KB
実行使用メモリ 4,828 KB
最終ジャッジ日時 2023-09-29 21:07:06
合計ジャッジ時間 1,878 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 8 ms
4,632 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 9 ms
4,576 KB
testcase_24 AC 9 ms
4,628 KB
testcase_25 AC 8 ms
4,828 KB
testcase_26 AC 7 ms
4,572 KB
testcase_27 AC 7 ms
4,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, j, n) for (int i = (j); i < (n); ++i)
using namespace std;
using ll = long long;
constexpr ll INF = 1LL << 60;
void chmin(ll& a, ll b) {
  if (a > b) a = b;
}

int n, a[10][10], p3[10], state[10];

void get_state(int S) {
  for (int i = 0; i < n; ++i) {
    state[i] = S % 3;
    S /= 3;
  }
}

int main() {
  cin >> n;
  rep(i, 0, n) rep(j, 0, n) cin >> a[i][j];
  p3[0] = 1;
  rep(i, 0, n + 1) p3[i + 1] = p3[i] * 3;
  
  vector<vector<ll>> dp(n, vector<ll>(p3[n], INF));
  rep(i, 0, n) dp[i][p3[i]] = 0;

  rep(S, 0, p3[n]) {
    get_state(S);
    rep(from, 0, n) {
      if (dp[from][S] == INF || state[from] == 0) continue;
      rep(to, 0, n) {
        if (from == to || state[to] == 2) continue;
        chmin(dp[to][S + p3[to]], dp[from][S] + a[from][to]);
      }
    }
  }
  
  ll ans = INF;
  rep(i, 0, n) chmin(ans, min(dp[i][p3[n] - 1], dp[i][p3[n] - 1 - p3[i]]));
  cout << ans << '\n';
  return 0;
}
0