結果

問題 No.1284 Flip Game
ユーザー KoDKoD
提出日時 2020-11-06 22:53:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 3,851 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 84,616 KB
実行使用メモリ 30,272 KB
最終ジャッジ日時 2023-09-29 19:22:43
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 4 ms
4,756 KB
testcase_16 AC 4 ms
4,800 KB
testcase_17 AC 11 ms
10,048 KB
testcase_18 AC 11 ms
10,296 KB
testcase_19 AC 41 ms
30,272 KB
testcase_20 AC 11 ms
10,080 KB
testcase_21 AC 11 ms
10,108 KB
testcase_22 AC 12 ms
10,060 KB
testcase_23 AC 42 ms
30,016 KB
testcase_24 AC 42 ms
30,008 KB
testcase_25 AC 43 ms
30,084 KB
testcase_26 AC 40 ms
30,168 KB
testcase_27 AC 42 ms
30,028 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:37:32: 警告: narrowing conversion of ‘(1 << N)’ from ‘int’ to ‘size_t’ {aka ‘long unsigned int’} [-Wnarrowing]
   37 |   struct iter {
      |                                ^   
main.cpp:37:40: 警告: narrowing conversion of ‘(1 << N)’ from ‘int’ to ‘size_t’ {aka ‘long unsigned int’} [-Wnarrowing]
   37 |   struct iter {
      |                                        ^   

ソースコード

diff #

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"

template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
  struct iter {
    std::size_t itr;
    constexpr iter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { ++itr; }
    constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  struct reviter {
    std::size_t itr;
    constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
    constexpr void operator ++ () noexcept { --itr; }
    constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
    constexpr std::size_t operator * () const noexcept { return itr; }
  };

  const iter first, last;

public:
  constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
  constexpr iter begin() const noexcept { return first; }
  constexpr iter end() const noexcept { return last; }
  constexpr reviter rbegin() const noexcept { return reviter(*last - 1); } 
  constexpr reviter rend() const noexcept { return reviter(*first - 1); } 
};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/multi_vec.cpp"

#include <cstddef>
#line 5 "/Users/kodamankod/Desktop/cpp_programming/Library/other/multi_vec.cpp"
#include <type_traits>

template <class T, size_t N, size_t I = 0>
decltype(auto) multi_vec(const size_t (&)[N], typename std::enable_if<(I == N), const T&>::type value = T{}) { 
  return value; 
}

template <class T, size_t N, size_t I = 0>
decltype(auto) multi_vec(const size_t (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T{}) { 
  return std::vector(list[I], multi_vec<T, N, I + 1>(list, value)); 
}

/**
 * @title Multi-Dimensional Vector
 */
#line 17 "main.cpp"

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  usize N;
  std::cin >> N;
  auto cost = multi_vec<u64>({ N, N });
  for (auto &x: cost) {
    for (auto &y: x) {
      std::cin >> y;
    }
  }
  auto dp = multi_vec<u64>({ 1 << N, 1 << N, N }, inf64);
  for (auto i: range(0, N)) {
    dp[0][0][i] = 0;
  }
  for (auto T: range(0, 1 << N)) {
    for (auto S: range(0, 1 << N)) {
      if (S & T) {
        continue;
      }
      for (auto p: range(0, N)) {
        if (T >> p & 1) {
          continue;
        }
        for (auto q: range(0, N)) {
          if (p == q || (T >> q & 1)) {
            continue;
          }
          if (S >> p & 1) {
            chmin(dp[S & ~(1 << p)][T | (1 << p)][q], dp[S][T][p] + cost[p][q]);
          }
          else {
            chmin(dp[S | (1 << p)][T][q], dp[S][T][p] + cost[p][q]);
          }
        }
      }
    }
  }
  u64 ans = inf64;
  for (auto T: range(0, 1 << N)) {
    for (auto S: range(0, 1 << N)) {
      for (auto p: range(0, N)) {
        if ((T | (1 << p)) == ((1 << N) - 1)) {
          chmin(ans, dp[S][T][p]);
        }
      }
    }
  }
  std::cout << ans << '\n';
  return 0;
}
0