結果

問題 No.1690 Power Grid
ユーザー mymelochanmymelochan
提出日時 2021-09-25 02:18:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 313 ms / 3,000 ms
コード長 1,867 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 87,312 KB
実行使用メモリ 79,064 KB
最終ジャッジ日時 2023-09-18 22:46:10
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,296 KB
testcase_01 AC 75 ms
71,140 KB
testcase_02 AC 75 ms
71,148 KB
testcase_03 AC 76 ms
71,460 KB
testcase_04 AC 76 ms
71,464 KB
testcase_05 AC 76 ms
71,276 KB
testcase_06 AC 144 ms
77,332 KB
testcase_07 AC 143 ms
77,404 KB
testcase_08 AC 145 ms
77,280 KB
testcase_09 AC 145 ms
77,312 KB
testcase_10 AC 94 ms
76,800 KB
testcase_11 AC 82 ms
76,232 KB
testcase_12 AC 76 ms
71,472 KB
testcase_13 AC 97 ms
76,604 KB
testcase_14 AC 170 ms
78,400 KB
testcase_15 AC 86 ms
76,244 KB
testcase_16 AC 306 ms
78,148 KB
testcase_17 AC 223 ms
79,064 KB
testcase_18 AC 183 ms
78,176 KB
testcase_19 AC 313 ms
78,140 KB
testcase_20 AC 256 ms
78,860 KB
testcase_21 AC 84 ms
76,292 KB
testcase_22 AC 180 ms
78,288 KB
testcase_23 AC 86 ms
76,356 KB
testcase_24 AC 161 ms
78,300 KB
testcase_25 AC 130 ms
77,540 KB
testcase_26 AC 95 ms
76,668 KB
testcase_27 AC 74 ms
71,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class dsu:

    def __init__(self,n):
        self.N = n
        self.cnt=[1]*self.N
        self.root=list(range(self.N))
        self.components = self.N

    def unite(self,x,y):
        x=self.leader(x)
        y=self.leader(y)
        if x!=y:
            self.components -= 1
            if self.cnt[x]<self.cnt[y]:
                x,y=y,x
            self.cnt[x]+=self.cnt[y]
            self.root[y]=x
            return x
        return None

    def leader(self,x):
        if self.root[x]==x:
            return x
        self.root[x]=self.leader(self.root[x])
        return self.root[x]

    def count_components(self):
        return self.components

N,M,K = map(int,input().split())
A = list(map(int,input().split()))
G = [[] for _ in range(N)]

for _ in range(M):
    x,y,z = map(int,input().split())
    x,y = x-1,y-1
    G[x].append((y,z))
    G[y].append((x,z))

def wf(G):
    N = len(G)
    INF = 10**20
    cost = [[INF]*N for _ in range(N)]
    for i in range(N):
        cost[i][i] = 0
    for s in range(N):
        for t,c in G[s]:
            cost[s][t] = c
            cost[t][s] = c
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if cost[i][k]!=INF and cost[k][j]!=INF:
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost

cost = wf(G)
E = []
for i in range(N):
    for j in range(i+1,N):
        E.append((i,j,cost[i][j]))
E.sort(key=lambda x:x[2])

from itertools import combinations

mi = 10**20
for V in combinations(range(N),K):
    tmp = 0
    f = [0]*N
    for v in V:
        tmp += A[v]
        f[v] = 1
    uft = dsu(N)
    cnt = K
    for x,y,z in E:
        if f[x] and f[y]:
            if not uft.unite(x,y) is None:
                cnt -= 1
                tmp += z
        if cnt == 1:
            break
    mi = min(mi,tmp)
print(mi)
0