結果

問題 No.1690 Power Grid
ユーザー zkouzkou
提出日時 2021-09-24 23:15:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,605 bytes
コンパイル時間 463 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 82,252 KB
最終ジャッジ日時 2023-09-18 22:20:41
合計ジャッジ時間 5,453 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
78,468 KB
testcase_01 AC 73 ms
71,224 KB
testcase_02 AC 76 ms
71,472 KB
testcase_03 AC 71 ms
71,364 KB
testcase_04 AC 73 ms
71,472 KB
testcase_05 AC 73 ms
71,228 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M, K = map(int, input().split())
As = list(map(int, input().split()))
XYZs = [tuple(map(int, input().split())) for _ in range(M)]

INF = 10 ** 18

adj = [[INF] * N for _ in range(N)]
for X, Y, Z in XYZs:
    X -= 1; Y -= 1
    adj[X][Y] = adj[Y][X] = min(adj[X][Y], Z)

for k in range(N):
    for i in range(N):
        for j in range(N):
            adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j])

def calc(indices):
    dp = [INF] * (1 << K)
    dp[0] = 0
    for i in range(K):
        dp[1 << i] = 0
    for s in range(1, 1 << K):
        for i in range(K):
            if not s & (1 << i):
                continue
            for j in range(K):
                if s & (1 << j):
                    continue
                dp[s | (1 << j)] = min(dp[s | (1 << j)], dp[s] + adj[indices[i]][indices[j]])
    return dp[-1]

def popcnt(n):
    "https://atcoder.jp/contests/abc152/submissions/9619555"
    c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff)
    return c


answer = INF
for s in range(1 << N):
    if popcnt(s) != K:
        continue
    indices = []
    tmp = 0
    for i in range(N):
        if s & (1 << i):
            indices.append(i)
            tmp += As[i]
    tmp += calc(indices)
    answer = min(answer, tmp)

print(answer)
0