結果

問題 No.1690 Power Grid
ユーザー ygd.ygd.
提出日時 2021-09-24 22:43:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 718 ms / 3,000 ms
コード長 1,444 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 81,472 KB
最終ジャッジ日時 2023-09-18 21:51:55
合計ジャッジ時間 13,531 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,216 KB
testcase_01 AC 73 ms
71,460 KB
testcase_02 AC 97 ms
76,292 KB
testcase_03 AC 73 ms
71,516 KB
testcase_04 AC 74 ms
71,260 KB
testcase_05 AC 73 ms
71,296 KB
testcase_06 AC 556 ms
81,052 KB
testcase_07 AC 557 ms
81,036 KB
testcase_08 AC 560 ms
81,100 KB
testcase_09 AC 556 ms
81,068 KB
testcase_10 AC 644 ms
80,924 KB
testcase_11 AC 553 ms
80,880 KB
testcase_12 AC 74 ms
71,372 KB
testcase_13 AC 95 ms
76,424 KB
testcase_14 AC 161 ms
77,500 KB
testcase_15 AC 669 ms
81,472 KB
testcase_16 AC 693 ms
81,116 KB
testcase_17 AC 364 ms
78,916 KB
testcase_18 AC 292 ms
78,252 KB
testcase_19 AC 703 ms
81,028 KB
testcase_20 AC 718 ms
81,452 KB
testcase_21 AC 689 ms
81,172 KB
testcase_22 AC 700 ms
81,104 KB
testcase_23 AC 718 ms
80,988 KB
testcase_24 AC 684 ms
80,924 KB
testcase_25 AC 671 ms
81,092 KB
testcase_26 AC 716 ms
81,008 KB
testcase_27 AC 72 ms
71,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
#input = sys.stdin.readline

def main():
    N,M,K = map(int,input().split()); INF = pow(10,18)
    A = list(map(int,input().split()))
    G = [[] for _ in range(N)]
    d = [[INF]*N for _ in range(N)]
    for _ in range(M):
        x,y,z = map(int,input().split())
        x -= 1; y -= 1
        G[x].append((z,y))
        G[y].append((z,x))
        d[x][y] = z; d[y][x] = z
        d[x][x] = 0; d[y][y] = 0
    
    n = len(d)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    #print(d)

    popcount = [0]*(1<<N)
    for i in range(1,1<<N):
        popcount[i] = popcount[i//2] + i%2

    dp = [INF]*(1<<N)
    dp[0] = 0

    ans = INF
    for i in range(1<<N):
        #今集合iに発電所があり、次にjに建てたい
        for j in range(N):
            if (i>>j)&1 == 1: continue #既にjに建っている
            if i == 0: #jが初めての発電所
                dp[i|1<<j] = A[j]
            else:
                cost = INF
                for k in range(N):
                    if (i>>k)&1 == 1:
                        cost = min(cost, d[j][k])
                #print(i,j,cost)
                dp[i|1<<j] = min(dp[i|1<<j], dp[i] + cost + A[j])
        if popcount[i] == K:
            ans = min(ans, dp[i])
    #print(dp)
    print(ans)

if __name__ == '__main__':
    main()
0