結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | Nzt3 |
提出日時 | 2024-02-11 16:28:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,955 ms / 2,500 ms |
コード長 | 2,151 bytes |
コンパイル時間 | 508 ms |
コンパイル使用メモリ | 82,372 KB |
実行使用メモリ | 162,116 KB |
最終ジャッジ日時 | 2024-09-28 17:35:23 |
合計ジャッジ時間 | 26,801 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 671 ms
99,472 KB |
testcase_01 | AC | 1,314 ms
121,864 KB |
testcase_02 | AC | 1,735 ms
153,360 KB |
testcase_03 | AC | 67 ms
74,068 KB |
testcase_04 | AC | 46 ms
64,684 KB |
testcase_05 | AC | 71 ms
76,664 KB |
testcase_06 | AC | 525 ms
87,220 KB |
testcase_07 | AC | 1,474 ms
157,768 KB |
testcase_08 | AC | 1,410 ms
148,876 KB |
testcase_09 | AC | 1,652 ms
158,496 KB |
testcase_10 | AC | 1,954 ms
161,920 KB |
testcase_11 | AC | 1,920 ms
162,116 KB |
testcase_12 | AC | 1,955 ms
162,088 KB |
testcase_13 | AC | 1,950 ms
161,824 KB |
testcase_14 | AC | 1,906 ms
161,824 KB |
testcase_15 | AC | 679 ms
80,696 KB |
testcase_16 | AC | 686 ms
80,948 KB |
testcase_17 | AC | 658 ms
80,884 KB |
testcase_18 | AC | 705 ms
100,592 KB |
testcase_19 | AC | 1,122 ms
139,668 KB |
testcase_20 | AC | 1,068 ms
128,696 KB |
testcase_21 | AC | 1,311 ms
158,708 KB |
ソースコード
class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 N,M=map(int,input().split()) E=[] for i in range(M): E.append(list(map(lambda x:int(x)-1,input().split()))) C=list(map(lambda x:int(x)-1,input().split())) W=list(map(int,input().split())) G=[dsu(N) for _ in range(1<<10)] for i in range(1<<10): for j in E: if (i>>C[j[0]]&1) and (i>>C[j[1]]&1): G[i].merge(j[0],j[1]) cost=[0]*(1<<10) for i in range(1<<10): for j in range(10): if i>>j&1: cost[i]+=W[j] Q=int(input()) for _ in range(Q): U,V=map(int,input().split()) U-=1 V-=1 ans=10**11 for i in range(1<<10): if G[i].same(U,V): ans=min(ans,cost[i]) if ans==10**11: ans=-1 print(ans)