結果

問題 No.1690 Power Grid
ユーザー asumo0729asumo0729
提出日時 2021-10-17 16:45:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,179 ms / 3,000 ms
コード長 3,032 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 81,560 KB
実行使用メモリ 78,112 KB
最終ジャッジ日時 2023-10-17 22:30:05
合計ジャッジ時間 10,416 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,648 KB
testcase_01 AC 46 ms
55,648 KB
testcase_02 AC 54 ms
64,144 KB
testcase_03 AC 46 ms
55,648 KB
testcase_04 AC 45 ms
55,648 KB
testcase_05 AC 46 ms
55,648 KB
testcase_06 AC 468 ms
76,660 KB
testcase_07 AC 470 ms
76,664 KB
testcase_08 AC 470 ms
76,664 KB
testcase_09 AC 471 ms
76,660 KB
testcase_10 AC 123 ms
76,044 KB
testcase_11 AC 124 ms
76,228 KB
testcase_12 AC 46 ms
55,648 KB
testcase_13 AC 87 ms
76,368 KB
testcase_14 AC 281 ms
77,936 KB
testcase_15 AC 111 ms
75,916 KB
testcase_16 AC 1,145 ms
77,444 KB
testcase_17 AC 722 ms
77,776 KB
testcase_18 AC 418 ms
78,112 KB
testcase_19 AC 1,113 ms
77,712 KB
testcase_20 AC 1,179 ms
78,012 KB
testcase_21 AC 117 ms
75,992 KB
testcase_22 AC 641 ms
77,972 KB
testcase_23 AC 119 ms
76,004 KB
testcase_24 AC 287 ms
77,432 KB
testcase_25 AC 204 ms
77,396 KB
testcase_26 AC 140 ms
76,424 KB
testcase_27 AC 47 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])

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
    
    adj = []
    for s in range(N - 1):
        for t in range(s + 1, N):
            if (bit >> s) & 1 and (bit >> t) & 1:
                adj.append((route[s][t], s, t))
    adj.sort()
    uf = union_find(N)
    for cost, x, y in adj:
        if not uf.same(x, y):
            temp += cost
            uf.union(x, y)
        if uf.size(x) == K:
            break
    ans = min(ans, temp)

print(ans)
0