結果

問題 No.1690 Power Grid
ユーザー ayaoniayaoni
提出日時 2021-09-24 22:47:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,023 ms / 3,000 ms
コード長 1,327 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 86,948 KB
実行使用メモリ 80,696 KB
最終ジャッジ日時 2023-09-18 21:53:57
合計ジャッジ時間 16,277 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,140 KB
testcase_01 AC 75 ms
71,344 KB
testcase_02 AC 105 ms
77,436 KB
testcase_03 AC 82 ms
71,380 KB
testcase_04 AC 77 ms
71,260 KB
testcase_05 AC 79 ms
71,252 KB
testcase_06 AC 657 ms
79,832 KB
testcase_07 AC 657 ms
79,904 KB
testcase_08 AC 647 ms
79,788 KB
testcase_09 AC 632 ms
80,128 KB
testcase_10 AC 868 ms
79,972 KB
testcase_11 AC 730 ms
80,144 KB
testcase_12 AC 77 ms
71,112 KB
testcase_13 AC 104 ms
76,908 KB
testcase_14 AC 197 ms
78,128 KB
testcase_15 AC 998 ms
79,636 KB
testcase_16 AC 695 ms
80,680 KB
testcase_17 AC 422 ms
79,104 KB
testcase_18 AC 278 ms
78,416 KB
testcase_19 AC 857 ms
79,916 KB
testcase_20 AC 735 ms
80,180 KB
testcase_21 AC 860 ms
79,604 KB
testcase_22 AC 962 ms
80,440 KB
testcase_23 AC 858 ms
79,584 KB
testcase_24 AC 1,023 ms
80,416 KB
testcase_25 AC 1,003 ms
80,340 KB
testcase_26 AC 931 ms
80,696 KB
testcase_27 AC 78 ms
71,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N,M,K = map(int,input().split())
A = list(map(int,input().split()))

inf = 10**18
dist = [[inf]*N for _ in range(N)]
for _ in range(M):
    X,Y,Z = map(int,input().split())
    X -= 1
    Y -= 1
    dist[X][Y] = Z
    dist[Y][X] = Z
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])


def bit_count(n):
    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


dp = [inf]*(1<<N)
for i in range(N):
    dp[1<<i] = A[i]

ans = inf
for i in range(1,1<<N):
    bc = bit_count(i)
    d = dp[i]
    if bc == K:
        ans = min(ans,d)
        continue
    already = []
    not_yet = []
    for j in range(N):
        if (i>>j) & 1:
            already.append(j)
        else:
            not_yet.append(j)
    for j in not_yet:
        dp[i^(1<<j)] = min(dp[i^(1<<j)],d+A[j]+min(dist[j][k] for k in already))

print(ans)
0