結果

問題 No.1690 Power Grid
ユーザー neterukunneterukun
提出日時 2021-09-24 22:33:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,307 ms / 3,000 ms
コード長 1,359 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 101,972 KB
最終ジャッジ日時 2023-09-18 21:47:57
合計ジャッジ時間 23,772 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,188 KB
testcase_01 AC 71 ms
71,500 KB
testcase_02 AC 103 ms
77,460 KB
testcase_03 AC 72 ms
71,472 KB
testcase_04 AC 72 ms
71,344 KB
testcase_05 AC 74 ms
71,432 KB
testcase_06 AC 1,231 ms
101,800 KB
testcase_07 AC 1,240 ms
101,708 KB
testcase_08 AC 1,239 ms
101,856 KB
testcase_09 AC 1,241 ms
101,808 KB
testcase_10 AC 1,239 ms
101,624 KB
testcase_11 AC 1,224 ms
101,656 KB
testcase_12 AC 73 ms
71,128 KB
testcase_13 AC 104 ms
77,472 KB
testcase_14 AC 233 ms
80,396 KB
testcase_15 AC 1,307 ms
101,840 KB
testcase_16 AC 1,274 ms
101,752 KB
testcase_17 AC 670 ms
90,164 KB
testcase_18 AC 360 ms
83,596 KB
testcase_19 AC 1,269 ms
101,880 KB
testcase_20 AC 1,291 ms
101,720 KB
testcase_21 AC 1,243 ms
101,852 KB
testcase_22 AC 1,275 ms
101,972 KB
testcase_23 AC 1,270 ms
101,556 KB
testcase_24 AC 1,278 ms
101,468 KB
testcase_25 AC 1,300 ms
101,852 KB
testcase_26 AC 1,301 ms
101,464 KB
testcase_27 AC 72 ms
71,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 10 ** 12


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)
dp[0] = 0

states = [bit_state for bit_state in range(1 << n)]
states = sorted(states, key=lambda x: bin(x).count("1"))

for bit_state in states:
    for new_v in range(n):
        if bit_state & (1 << new_v):
            continue
        new_state = bit_state | (1 << new_v)
        dp[new_state] = min(dp[new_state],
                            dp[bit_state] + a[new_v] + cost(bit_state, new_v))

ans = INF
for bit_state in states:
    if bin(bit_state).count("1") == k:
        ans = min(ans, dp[bit_state])
print(ans)
0