結果

問題 No.1690 Power Grid
ユーザー asumo0729asumo0729
提出日時 2021-10-17 16:56:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 335 ms / 3,000 ms
コード長 3,012 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 77,772 KB
最終ジャッジ日時 2023-10-17 22:30:11
合計ジャッジ時間 5,357 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,648 KB
testcase_01 AC 46 ms
55,648 KB
testcase_02 AC 54 ms
64,144 KB
testcase_03 AC 45 ms
55,648 KB
testcase_04 AC 46 ms
55,648 KB
testcase_05 AC 45 ms
55,648 KB
testcase_06 AC 175 ms
76,832 KB
testcase_07 AC 176 ms
76,832 KB
testcase_08 AC 175 ms
76,836 KB
testcase_09 AC 174 ms
76,836 KB
testcase_10 AC 121 ms
76,048 KB
testcase_11 AC 121 ms
76,004 KB
testcase_12 AC 46 ms
55,648 KB
testcase_13 AC 79 ms
74,204 KB
testcase_14 AC 171 ms
77,660 KB
testcase_15 AC 111 ms
75,936 KB
testcase_16 AC 335 ms
77,748 KB
testcase_17 AC 255 ms
77,684 KB
testcase_18 AC 195 ms
77,664 KB
testcase_19 AC 321 ms
77,648 KB
testcase_20 AC 327 ms
77,772 KB
testcase_21 AC 118 ms
76,048 KB
testcase_22 AC 221 ms
77,096 KB
testcase_23 AC 119 ms
76,016 KB
testcase_24 AC 214 ms
77,160 KB
testcase_25 AC 172 ms
76,608 KB
testcase_26 AC 131 ms
76,300 KB
testcase_27 AC 46 ms
55,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 = 1 << 60
inf1 = float('inf')
eps = 1e-9
sortkey1 = itemgetter(0)
sortkey2 = lambda x: (x[0], x[1])

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()
route = [[inf1 for _ in range(N)] for _ in range(N)]

for _ in range(M):
    x, y, z = lp()
    x -= 1; y -= 1
    route[x][y] = z
    route[y][x] = z
for i in range(N):
    route[i][i] = 0

for mid in range(N):
    for s in range(N):
        for g in range(N):
            route[s][g] = min(route[s][g], route[s][mid] + route[mid][g])
adj = []
for s in range(N - 1):
        for t in range(s + 1, N):
            adj.append((route[s][t], s, t))

adj.sort()

ans = inf1

for bit in range(2 ** N):
    one = 0
    temp = 0
    for i in range(N):
        if (bit >> i) & 1:
            one += 1
            temp += A[i]
    if one != K:
        continue

    uf = union_find(N)
    for cost, x, y in adj:
        if (bit >> x) & 1 and (bit >> y) & 1   and  not uf.same(x, y):
            temp += cost
            uf.union(x, y)
            if uf.size(x) == K:
                break
    ans = min(ans, temp)

print(ans)
0