結果

問題 No.1690 Power Grid
ユーザー vwxyzvwxyz
提出日時 2023-12-01 15:41:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,691 ms / 3,000 ms
コード長 7,579 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 81,468 KB
最終ジャッジ日時 2023-12-01 15:41:54
合計ジャッジ時間 17,543 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,604 KB
testcase_01 AC 43 ms
55,604 KB
testcase_02 AC 68 ms
70,012 KB
testcase_03 AC 43 ms
55,604 KB
testcase_04 AC 42 ms
55,604 KB
testcase_05 AC 43 ms
55,604 KB
testcase_06 AC 1,174 ms
81,464 KB
testcase_07 AC 1,160 ms
81,468 KB
testcase_08 AC 1,144 ms
81,464 KB
testcase_09 AC 1,137 ms
81,464 KB
testcase_10 AC 265 ms
76,596 KB
testcase_11 AC 340 ms
77,600 KB
testcase_12 AC 42 ms
55,604 KB
testcase_13 AC 120 ms
77,064 KB
testcase_14 AC 420 ms
78,436 KB
testcase_15 AC 242 ms
76,976 KB
testcase_16 AC 1,666 ms
80,360 KB
testcase_17 AC 978 ms
80,224 KB
testcase_18 AC 573 ms
79,064 KB
testcase_19 AC 1,671 ms
80,144 KB
testcase_20 AC 1,691 ms
80,204 KB
testcase_21 AC 338 ms
76,476 KB
testcase_22 AC 994 ms
80,952 KB
testcase_23 AC 247 ms
76,468 KB
testcase_24 AC 548 ms
78,772 KB
testcase_25 AC 388 ms
78,256 KB
testcase_26 AC 320 ms
78,140 KB
testcase_27 AC 47 ms
55,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=0
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components
    
    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)            
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

class Graph:
    def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if graph!=None:
            self.graph=graph
            """
            self.edges=[]
            for i in range(self.V):
                if self.weighted:
                    for j,d in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j,d))
                else:
                    for j in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j))
            """
        else:
            self.edges=edges
            self.graph=[[] for i in range(self.V)]
            if weighted:
                for i,j,d in self.edges:
                    self.graph[i].append((j,d))
                    if not self.directed:
                        self.graph[j].append((i,d))
            else:
                for i,j in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)

    def Warshall_Floyd(self,route_restoration=False):
        dist=[[self.inf]*self.V for i in range(self.V)]
        for i in range(self.V):
            dist[i][i]=0
        if route_restoration:
            parents=[[j for j in range(self.V)] for i in range(self.V)]
        for i,j,d in self.edges:
            if i==j:
                continue
            if dist[i][j]>d:
                dist[i][j]=d
                if route_restoration:
                    parents[i][j]=i
            if not self.directed and dist[j][i]>d:
                dist[j][i]=d
                if route_restoration:
                    parents[j][i]=j
        for k in range(self.V):
            for i in range(self.V):
                for j in range(self.V):
                    if dist[i][j]>dist[i][k]+dist[k][j]:
                        dist[i][j]=dist[i][k]+dist[k][j]
                        if route_restoration:
                            parents[i][j]=parents[k][j]
        for i in range(self.V):
            if dist[i][i]<0:
                for j in range(self.V):
                    if dist[i][j]!=self.inf:
                        dist[i][j]=-self.inf
        if route_restoration:
            for i in range(self.V):
                if dist[i][i]==0:
                    parents[i][i]=None
            return dist,parents
        else:
            return dist

    def Kruskal(self,maximize=False,spanning_tree=False):
        UF=UnionFind(self.V)
        sorted_edges=sorted(self.edges if self.weighted else [(x,y,1) for x,y in self.edges],key=lambda tpl:tpl[2],reverse=maximize)
        if spanning_tree:
            st=[]
        else:
            cost=0
        for x,y,d in sorted_edges:
            if not UF.Same(x,y):
                UF.Union(x,y)
                if spanning_tree:
                    st.append((x,y,d))
                else:
                    cost+=d
        return st if spanning_tree else cost

N,M,K=map(int,readline().split())
A=list(map(int,readline().split()))
edges=[]
for m in range(M):
    x,y,z=map(int,readline().split())
    x-=1;y-=1
    edges.append((x,y,z))
G=Graph(N,edges=edges,weighted=True)
dist=G.Warshall_Floyd()
inf=1<<60
ans=inf
for bit in range(1<<N):
    if sum(1 for i in range(N) if bit&1<<i)==K:
        edges=[]
        for i in range(N):
            for j in range(i+1,N):
                if bit&1<<i and bit&1<<j:
                    edges.append((i,j,dist[i][j]))
        ans=min(ans,sum(A[i] for i in range(N) if bit&1<<i)+Graph(N,edges=edges,weighted=True).Kruskal())
print(ans)
0