結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-01 00:34:45 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 817 ms / 3,000 ms |
| コード長 | 1,287 bytes |
| コンパイル時間 | 605 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 77,996 KB |
| 最終ジャッジ日時 | 2024-09-25 17:48:52 |
| 合計ジャッジ時間 | 9,222 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
if N == 1:
print(min(A))
exit(0)
INF = 1 << 60
dist = [[INF] * N for i in range(N)]
for i in range(N):
dist[i][i] = 0
for _ in range(M):
x,y,z = map(int,input().split())
dist[x - 1][y - 1] = z
dist[y - 1][x - 1] = 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 popcnt(x):
res = 0
while x:
res += (x & 1)
x >>= 1
return res
import heapq as hq
ans = INF
for status in range(1 << N):
if popcnt(status) != K:
continue
elec = set()
first = -1
for i in range(N):
if (status >> i) & 1:
elec.add(i)
first = i
q = []
hq.heappush(q, (0, first))
visited = [False] * N
cost = 0
while q:
c, v = hq.heappop(q)
if visited[v]:
continue
visited[v] = True
cost += c
cost += A[v]
for i in range(N):
if i not in elec:
continue
if visited[i]:
continue
hq.heappush(q, (dist[v][i], i))
ans = min(ans, cost)
print(ans)