結果

問題 No.1690 Power Grid
ユーザー vwxyzvwxyz
提出日時 2023-12-01 15:41:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,340 ms / 3,000 ms
コード長 7,579 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 81,864 KB
最終ジャッジ日時 2024-09-26 15:42:00
合計ジャッジ時間 14,070 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,424 KB
testcase_01 AC 40 ms
55,552 KB
testcase_02 AC 60 ms
69,632 KB
testcase_03 AC 40 ms
55,680 KB
testcase_04 AC 39 ms
55,168 KB
testcase_05 AC 40 ms
55,424 KB
testcase_06 AC 921 ms
81,864 KB
testcase_07 AC 914 ms
81,484 KB
testcase_08 AC 939 ms
81,608 KB
testcase_09 AC 967 ms
81,352 KB
testcase_10 AC 234 ms
77,360 KB
testcase_11 AC 288 ms
77,872 KB
testcase_12 AC 43 ms
55,936 KB
testcase_13 AC 113 ms
77,736 KB
testcase_14 AC 373 ms
79,096 KB
testcase_15 AC 220 ms
77,564 KB
testcase_16 AC 1,298 ms
80,752 KB
testcase_17 AC 783 ms
79,972 KB
testcase_18 AC 494 ms
79,464 KB
testcase_19 AC 1,324 ms
80,524 KB
testcase_20 AC 1,340 ms
81,372 KB
testcase_21 AC 238 ms
76,800 KB
testcase_22 AC 744 ms
81,424 KB
testcase_23 AC 207 ms
77,036 KB
testcase_24 AC 442 ms
79,232 KB
testcase_25 AC 337 ms
78,408 KB
testcase_26 AC 295 ms
78,592 KB
testcase_27 AC 41 ms
55,168 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