結果

問題 No.1690 Power Grid
ユーザー mymelochan
提出日時 2021-09-25 02:17:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,841 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-07-05 11:55:06
合計ジャッジ時間 3,074 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 14
権限があれば一括ダウンロードができます

ソースコード

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]))

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