結果

問題 No.1690 Power Grid
ユーザー neterukunneterukun
提出日時 2021-09-24 22:15:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,466 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 153,800 KB
最終ジャッジ日時 2023-09-18 21:34:25
合計ジャッジ時間 48,481 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,244 KB
testcase_01 AC 77 ms
71,120 KB
testcase_02 AC 97 ms
77,308 KB
testcase_03 AC 79 ms
71,432 KB
testcase_04 AC 74 ms
71,272 KB
testcase_05 AC 76 ms
71,396 KB
testcase_06 AC 2,426 ms
135,164 KB
testcase_07 AC 2,419 ms
135,348 KB
testcase_08 AC 2,408 ms
135,200 KB
testcase_09 AC 2,413 ms
135,284 KB
testcase_10 AC 1,677 ms
120,640 KB
testcase_11 TLE -
testcase_12 AC 75 ms
71,300 KB
testcase_13 AC 112 ms
77,632 KB
testcase_14 AC 374 ms
84,076 KB
testcase_15 TLE -
testcase_16 AC 2,511 ms
135,308 KB
testcase_17 AC 1,273 ms
105,516 KB
testcase_18 AC 663 ms
90,656 KB
testcase_19 AC 2,500 ms
135,032 KB
testcase_20 AC 2,496 ms
135,084 KB
testcase_21 AC 1,625 ms
118,540 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 76 ms
71,244 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)


costs = [[INF] * (1 << n) for i in range(n)]
for v in range(n):
    for bit_state in range(1 << n):
        costs[v][bit_state] = cost(bit_state, v)

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] + costs[new_v][bit_state])

print(min(dp[k]))
0