結果

問題 No.1690 Power Grid
ユーザー asumo0729asumo0729
提出日時 2021-09-25 00:29:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 375 ms / 3,000 ms
コード長 3,086 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 87,216 KB
実行使用メモリ 80,608 KB
最終ジャッジ日時 2023-09-18 22:33:19
合計ジャッジ時間 6,170 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
72,452 KB
testcase_01 AC 97 ms
72,364 KB
testcase_02 AC 98 ms
72,472 KB
testcase_03 AC 99 ms
71,740 KB
testcase_04 AC 94 ms
71,796 KB
testcase_05 AC 97 ms
71,840 KB
testcase_06 AC 179 ms
78,072 KB
testcase_07 AC 179 ms
78,220 KB
testcase_08 AC 180 ms
78,140 KB
testcase_09 AC 178 ms
78,236 KB
testcase_10 AC 120 ms
77,600 KB
testcase_11 AC 105 ms
77,328 KB
testcase_12 AC 98 ms
72,260 KB
testcase_13 AC 124 ms
77,412 KB
testcase_14 AC 207 ms
79,892 KB
testcase_15 AC 106 ms
77,252 KB
testcase_16 AC 370 ms
78,900 KB
testcase_17 AC 280 ms
80,608 KB
testcase_18 AC 238 ms
79,484 KB
testcase_19 AC 375 ms
79,204 KB
testcase_20 AC 319 ms
80,176 KB
testcase_21 AC 109 ms
77,640 KB
testcase_22 AC 216 ms
79,340 KB
testcase_23 AC 114 ms
77,580 KB
testcase_24 AC 198 ms
80,064 KB
testcase_25 AC 157 ms
78,516 KB
testcase_26 AC 119 ms
77,512 KB
testcase_27 AC 97 ms
72,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import itemgetter
from collections import defaultdict, deque
import heapq
import bisect
from itertools import combinations, product

stdin=sys.stdin
sys.setrecursionlimit(10 ** 8)

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
Yp=lambda:print('Yes')
Np=lambda:print('No')
inf = float('inf')
eps = 1e-9
sortkey = itemgetter(0)

from collections import Counter

class union_find():
    def __init__(self,n):
        self.n=n
        ##親要素のノード番号を格納。par[x]==xのときそのノードは根
        ##親とはその上にノードなし!! 
        self.par=[-1 for i in range(n)]
        self.rank=[0]*(n)

    def find(self,x):
        if self.par[x]<0:
            return x
        else:
            self.par[x]=self.find(self.par[x])
            
            return self.par[x]

    def union(self,x,y):
        x=self.find(x)
        y=self.find(y)

        ##木の高さを比較し、低い方から高い方へ辺をはる
        if x==y:
          return

        if self.par[x]>self.par[y]:
          x,y=y,x
          
        self.par[x]+=self.par[y]
        self.par[y]=x

    ##2つが同じ親かどうか
    def same(self,x,y):
        return self.find(x) == self.find(y)
    
    ##所属のサイズ
    def size(self,x):
      return -self.par[self.find(x)]
      
    ##同じ親のものを表示
    def members(self,x):
      root=self.find(x)
      return [i for i in range(self.n) if self.find(i)==root]
    
    ##親を表示!!
    def roots(self):
      return [i for i, x in enumerate(self.par) if x<0]
    
    def all_group_member(self):
      return {r:self.members(r) for r in self.roots()}

    def group_count(self):
      return len(self.roots())

## uf memo
## 素集合に分けるが、各集合には親というIDが振られているという意識を持つ!!(ABC049_Dより)

###############################################################

N, M, K = lp()
A = lp()
edges = [[inf for _ in range(N)] for _ in range(N)]
for i in range(N):
    edges[i][i] = 0

for _ in range(M):
    x, y, z = lp()
    x -= 1; y -= 1
    edges[x][y] = z
    edges[y][x] = z

for mid in range(N):
    for s in range(N):
        for g in range(N):
            dist = min(edges[s][g], edges[s][mid] + edges[mid][g])
            edges[s][g] = dist

adj = [[edges[i][j], i, j]for i, j in product(range(N), repeat=2) if i < j]
adj.sort(key=lambda x:x[0])


citys = [i for i in range(N)]
ans = inf
for v in combinations(citys,K):
    uf = union_find(N)
    temp = 0
    candidate = [0 for _ in range(N)]
    cnt = 0
    for city in v:
        temp += A[city]
        candidate[city] = 1
    if cnt == K - 1:
        ans = min(ans, temp)
        continue

    for cost, x, y in adj:
        if candidate[x] == 1 and candidate[y] == 1 and not uf.same(x, y):
            uf.union(x, y)
            temp += cost
            cnt += 1

        if cnt == K - 1:
            ans = min(ans, temp)
            break

print(ans)
0