結果

問題 No.1690 Power Grid
ユーザー ayaoniayaoni
提出日時 2021-09-24 23:27:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,019 ms / 3,000 ms
コード長 1,322 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 80,776 KB
最終ジャッジ日時 2023-09-18 22:24:54
合計ジャッジ時間 13,687 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,428 KB
testcase_01 AC 76 ms
71,484 KB
testcase_02 AC 76 ms
71,404 KB
testcase_03 AC 74 ms
71,376 KB
testcase_04 AC 74 ms
71,512 KB
testcase_05 AC 74 ms
71,392 KB
testcase_06 AC 435 ms
79,764 KB
testcase_07 AC 412 ms
80,116 KB
testcase_08 AC 419 ms
80,080 KB
testcase_09 AC 419 ms
79,772 KB
testcase_10 AC 90 ms
78,476 KB
testcase_11 AC 743 ms
80,364 KB
testcase_12 AC 76 ms
71,436 KB
testcase_13 AC 98 ms
76,496 KB
testcase_14 AC 198 ms
78,224 KB
testcase_15 AC 1,015 ms
79,568 KB
testcase_16 AC 440 ms
80,452 KB
testcase_17 AC 330 ms
79,060 KB
testcase_18 AC 218 ms
78,872 KB
testcase_19 AC 514 ms
80,184 KB
testcase_20 AC 471 ms
80,060 KB
testcase_21 AC 87 ms
78,576 KB
testcase_22 AC 960 ms
80,388 KB
testcase_23 AC 860 ms
79,568 KB
testcase_24 AC 1,019 ms
80,776 KB
testcase_25 AC 1,006 ms
80,124 KB
testcase_26 AC 943 ms
80,608 KB
testcase_27 AC 75 ms
71,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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:
        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