結果

問題 No.1284 Flip Game
ユーザー ああいいああいい
提出日時 2022-02-19 14:55:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 695 ms / 2,000 ms
コード長 1,334 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 122,212 KB
最終ジャッジ日時 2023-09-11 20:40:36
合計ジャッジ時間 7,972 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,272 KB
testcase_01 AC 80 ms
71,328 KB
testcase_02 AC 146 ms
78,708 KB
testcase_03 AC 79 ms
71,548 KB
testcase_04 AC 80 ms
71,488 KB
testcase_05 AC 80 ms
71,468 KB
testcase_06 AC 79 ms
71,416 KB
testcase_07 AC 80 ms
71,328 KB
testcase_08 AC 79 ms
71,284 KB
testcase_09 AC 81 ms
71,624 KB
testcase_10 AC 81 ms
71,664 KB
testcase_11 AC 113 ms
78,016 KB
testcase_12 AC 114 ms
77,884 KB
testcase_13 AC 146 ms
79,040 KB
testcase_14 AC 145 ms
78,980 KB
testcase_15 AC 174 ms
80,292 KB
testcase_16 AC 161 ms
80,448 KB
testcase_17 AC 208 ms
87,608 KB
testcase_18 AC 206 ms
87,608 KB
testcase_19 AC 411 ms
118,316 KB
testcase_20 AC 251 ms
88,780 KB
testcase_21 AC 222 ms
88,632 KB
testcase_22 AC 224 ms
88,632 KB
testcase_23 AC 664 ms
121,656 KB
testcase_24 AC 695 ms
121,700 KB
testcase_25 AC 653 ms
122,212 KB
testcase_26 AC 412 ms
118,532 KB
testcase_27 AC 499 ms
120,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
c = [list(map(int,input().split())) for _ in range(N)]

inf = 10 ** 11
ans = 10 ** 11
import heapq
def pack(S,T,j):
    return (S << 13) + (T << 4) + j
def unpack(z):
    S = z >> 13
    z %= (1 << 13)
    T = z >> 4
    j = z % (1 << 4)
    return (S,T,j)
if True:
    dp = [[[inf] * N for _ in range(1 << N)] for _ in range(1 << N)]
    q = []
    for i in range(N):
        dp[1 << i][0][i] = 0
        q.append((0,pack(1 << i,0,i)))
    while q:
        d,z = heapq.heappop(q)
        S,T,last = unpack(z)
        if S == (1 << N) - 1:break
        maskl = 1 << last
        for j in range(N):
            maskj = 1 << j
            if S & maskj:continue
            if T & maskl:
                if dp[S | maskj][T][j] > d + c[last][j]:
                    dp[S | maskj][T][j] = d + c[last][j]
                    heapq.heappush(q,(d + c[last][j],pack(S|maskj,T,j)))
            else:
                bit =(S | maskj) ^ maskl
                if dp[bit][T | maskl][j] > d + c[last][j]:
                    dp[bit][T | maskl][j] = d + c[last][j]
                    heapq.heappush(q,(d + c[last][j],pack(bit,T | maskl,j)))
    _min = inf
    for T in range(1 << N):
        for j in range(N):
            if dp[-1][T][j] < _min:
                _min = dp[-1][T][j]
    if _min < ans:
        ans = _min
print(ans)
0