結果
問題 | No.1690 Power Grid |
ユーザー |
![]() |
提出日時 | 2021-09-24 22:52:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 975 ms / 3,000 ms |
コード長 | 1,274 bytes |
コンパイル時間 | 150 ms |
コンパイル使用メモリ | 82,272 KB |
実行使用メモリ | 79,296 KB |
最終ジャッジ日時 | 2024-07-05 11:08:01 |
合計ジャッジ時間 | 7,489 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
import sys from itertools import combinations as combi input = sys.stdin.readline N, M, K = map(int, input().split()) a = list(map(int, input().split())) d = [[10 ** 18] * N for _ in range(N)] for i in range(M): u, v, c = map(int, input().split()) d[u - 1][v - 1] = c d[v - 1][u - 1] = c 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]) import heapq class prim: def __init__(self, n, e): self.e = e self.n = n def MSTcost(self): h = [] visited = [0] * (self.n + 1) ks = range(K) b = pow(10, 10) for edge in self.e[ks[0]]: heapq.heappush(h, edge[1] * b + edge[0]) res = 0 visited[ks[0]] = 1 while len(h): p = heapq.heappop(h) p0 = p // b p1 = p % b if visited[p1]: continue visited[p1] = 1 for q in self.e[p1]: if visited[q[0]]: continue heapq.heappush(h, q[1] * b + q[0]) res += p0 return res res = 10 ** 18 for c in combi(range(N), K): cres = 0 e = [[] for _ in range(K)] for i in range(K): for j in range(K): e[i].append((j, d[c[i]][c[j]])) pri = prim(N, e) cres += pri.MSTcost() for i in c: cres += a[i] res = min(res, cres) #print(c, cres, d) print(res)