結果

問題 No.1690 Power Grid
ユーザー neterukunneterukun
提出日時 2021-09-24 22:10:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,864 ms / 3,000 ms
コード長 1,313 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 116,900 KB
最終ジャッジ日時 2023-09-18 21:28:56
合計ジャッジ時間 32,561 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,164 KB
testcase_01 AC 72 ms
71,392 KB
testcase_02 AC 75 ms
71,612 KB
testcase_03 AC 72 ms
71,076 KB
testcase_04 AC 74 ms
71,416 KB
testcase_05 AC 75 ms
71,240 KB
testcase_06 AC 1,431 ms
98,420 KB
testcase_07 AC 1,411 ms
98,348 KB
testcase_08 AC 1,420 ms
98,204 KB
testcase_09 AC 1,408 ms
98,448 KB
testcase_10 AC 300 ms
83,680 KB
testcase_11 AC 2,680 ms
114,904 KB
testcase_12 AC 71 ms
71,380 KB
testcase_13 AC 105 ms
77,492 KB
testcase_14 AC 272 ms
80,272 KB
testcase_15 AC 2,864 ms
116,612 KB
testcase_16 AC 1,425 ms
98,452 KB
testcase_17 AC 780 ms
88,448 KB
testcase_18 AC 377 ms
82,032 KB
testcase_19 AC 1,437 ms
98,316 KB
testcase_20 AC 1,445 ms
98,748 KB
testcase_21 AC 195 ms
81,356 KB
testcase_22 AC 2,273 ms
106,676 KB
testcase_23 AC 2,829 ms
116,900 KB
testcase_24 AC 2,529 ms
110,880 KB
testcase_25 AC 2,651 ms
112,816 KB
testcase_26 AC 2,735 ms
114,784 KB
testcase_27 AC 73 ms
71,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 10 ** 18


def warshall_floyd(matrix):
    n = len(matrix)
    dist = [[d for d in row] for row in matrix]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    return dist


def cost(state, v):
    res = INF
    for nxt_v in range(n):
        if state & (1 << nxt_v):
            res = min(res, dist[v][nxt_v])
    if res == INF:
        return 0
    else:
        return res


n, m, k = map(int, input().split())
a = list(map(int, input().split()))
edges = [list(map(int, input().split())) for i in range(m)]


matrix = [[INF] * n for i in range(n)]
for i in range(n):
    matrix[i][i] = 0
for u, v, c in edges:
    u -= 1
    v -= 1
    matrix[u][v] = c
    matrix[v][u] = c
dist = warshall_floyd(matrix)

dp = [[INF] * (1 << n) for i in range(k + 1)]
dp[0][0] = 0

for i in range(k):
    for bit_state in range(1 << n):
        if bin(bit_state).count("1") != i:
            continue
        for new_v in range(n):
            if bit_state & (1 << new_v):
                continue
            new_state = bit_state | (1 << new_v)
            dp[i + 1][new_state] = min(dp[i + 1][new_state],
                                       dp[i][bit_state] + a[new_v] + cost(bit_state, new_v))

print(min(dp[k]))
0