結果

問題 No.1690 Power Grid
ユーザー asumo0729asumo0729
提出日時 2021-09-25 00:02:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,206 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,404 KB
実行使用メモリ 80,624 KB
最終ジャッジ日時 2023-09-18 22:30:13
合計ジャッジ時間 6,152 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,864 KB
testcase_01 AC 96 ms
72,172 KB
testcase_02 AC 97 ms
72,444 KB
testcase_03 AC 98 ms
71,772 KB
testcase_04 AC 94 ms
71,764 KB
testcase_05 AC 94 ms
71,768 KB
testcase_06 AC 179 ms
78,232 KB
testcase_07 AC 180 ms
77,884 KB
testcase_08 AC 179 ms
77,980 KB
testcase_09 AC 181 ms
78,296 KB
testcase_10 AC 125 ms
77,800 KB
testcase_11 AC 114 ms
77,544 KB
testcase_12 AC 99 ms
72,460 KB
testcase_13 AC 119 ms
77,492 KB
testcase_14 AC 198 ms
79,520 KB
testcase_15 AC 107 ms
77,288 KB
testcase_16 AC 377 ms
80,624 KB
testcase_17 AC 280 ms
79,600 KB
testcase_18 AC 249 ms
79,816 KB
testcase_19 AC 343 ms
80,036 KB
testcase_20 AC 351 ms
80,440 KB
testcase_21 AC 114 ms
77,636 KB
testcase_22 WA -
testcase_23 AC 117 ms
77,368 KB
testcase_24 AC 200 ms
80,044 KB
testcase_25 AC 161 ms
78,664 KB
testcase_26 AC 123 ms
77,504 KB
testcase_27 AC 96 ms
72,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 s in range(N):
    for g in range(N):
        for mid in range(N):
            if s == mid or g == mid or s == g:
                continue
            dist = min(edges[s][g], edges[s][mid] + edges[mid][g])
            edges[s][g] = dist

adj = []
for i in range(N - 1):
    for j in range(i + 1, N):
        adj.append((edges[i][j], i, j))
adj.sort()

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:
            if not uf.same(x, y):
                uf.union(x, y)
                temp += cost
                cnt += 1

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

print(ans)
0