結果

問題 No.1284 Flip Game
ユーザー 👑 tamatotamato
提出日時 2020-11-06 22:42:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,854 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 87,396 KB
実行使用メモリ 114,732 KB
最終ジャッジ日時 2023-09-29 19:17:26
合計ジャッジ時間 4,913 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,484 KB
testcase_01 AC 74 ms
71,116 KB
testcase_02 AC 106 ms
77,804 KB
testcase_03 AC 73 ms
71,168 KB
testcase_04 AC 76 ms
71,560 KB
testcase_05 AC 74 ms
71,120 KB
testcase_06 AC 74 ms
71,084 KB
testcase_07 AC 73 ms
71,472 KB
testcase_08 AC 74 ms
71,392 KB
testcase_09 AC 77 ms
75,852 KB
testcase_10 AC 79 ms
76,272 KB
testcase_11 AC 90 ms
76,428 KB
testcase_12 AC 90 ms
76,704 KB
testcase_13 AC 106 ms
78,012 KB
testcase_14 AC 107 ms
77,608 KB
testcase_15 AC 113 ms
78,872 KB
testcase_16 AC 114 ms
79,072 KB
testcase_17 AC 127 ms
85,276 KB
testcase_18 AC 128 ms
85,640 KB
testcase_19 AC 192 ms
114,280 KB
testcase_20 AC 130 ms
85,740 KB
testcase_21 AC 129 ms
85,572 KB
testcase_22 AC 129 ms
85,968 KB
testcase_23 AC 199 ms
114,124 KB
testcase_24 AC 197 ms
114,416 KB
testcase_25 AC 201 ms
114,256 KB
testcase_26 AC 194 ms
114,216 KB
testcase_27 AC 196 ms
114,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
inf = 10**17


def main():
    import sys
    input = sys.stdin.readline

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

    dp = [[inf] * (1 << (N*2)) for _ in range(N*2)]
    for i in range(N):
        dp[i][1 << i] = 0
    for state in range(1 << (N*2)):
        for v_last in range(N*2):
            if dp[v_last][state] == inf:
                continue
            for v_new in range(N):
                if not state >> v_new & 1:
                    state_new = state | (1 << v_new)
                    if v_last < N:
                        dp[v_new][state_new] = min(dp[v_new][state_new], dp[v_last][state] + C[v_last][v_new])
                    else:
                        dp[v_new][state_new] = min(dp[v_new][state_new], dp[v_last][state] + C[v_last - N][v_new])
                else:
                    v_new2 = v_new + N
                    if not state >> v_new2 & 1:
                        if v_last + N != v_new2:
                            state_new2 = state | (1 << v_new2)
                            if v_last < N:
                                dp[v_new2][state_new2] = min(dp[v_new2][state_new2], dp[v_last][state] + C[v_last][v_new])
                            else:
                                dp[v_new2][state_new2] = min(dp[v_new2][state_new2],
                                                             dp[v_last][state] + C[v_last - N][v_new])
    if N == 2:
        ans = inf
        for v in range(N, N*2):
            ans = min(ans, dp[v][-1])
        print(ans)
    else:
        ans = inf
        for v in range(N, N*2):
            ans = min(ans, dp[v][-1])
        for vv in range(N):
            ans = min(ans, dp[vv][((1 << (2*N)) - 1) ^ (1 << (vv + N))])
        print(ans)


if __name__ == '__main__':
    main()
0