結果
問題 | No.1814 Uribo Road (Easy) |
ユーザー | MasKoaTS |
提出日時 | 2022-01-12 22:09:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 634 ms / 2,000 ms |
コード長 | 3,272 bytes |
コンパイル時間 | 208 ms |
コンパイル使用メモリ | 82,084 KB |
実行使用メモリ | 100,308 KB |
最終ジャッジ日時 | 2024-11-21 23:01:32 |
合計ジャッジ時間 | 12,129 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
53,376 KB |
testcase_01 | AC | 49 ms
54,016 KB |
testcase_02 | AC | 47 ms
53,248 KB |
testcase_03 | AC | 47 ms
52,992 KB |
testcase_04 | AC | 46 ms
53,248 KB |
testcase_05 | AC | 46 ms
53,504 KB |
testcase_06 | AC | 46 ms
53,120 KB |
testcase_07 | AC | 46 ms
53,632 KB |
testcase_08 | AC | 46 ms
53,504 KB |
testcase_09 | AC | 134 ms
76,672 KB |
testcase_10 | AC | 122 ms
76,416 KB |
testcase_11 | AC | 149 ms
77,312 KB |
testcase_12 | AC | 182 ms
78,080 KB |
testcase_13 | AC | 170 ms
77,956 KB |
testcase_14 | AC | 214 ms
78,360 KB |
testcase_15 | AC | 113 ms
76,416 KB |
testcase_16 | AC | 112 ms
76,160 KB |
testcase_17 | AC | 137 ms
76,544 KB |
testcase_18 | AC | 99 ms
76,132 KB |
testcase_19 | AC | 81 ms
70,784 KB |
testcase_20 | AC | 263 ms
81,408 KB |
testcase_21 | AC | 171 ms
78,160 KB |
testcase_22 | AC | 380 ms
88,516 KB |
testcase_23 | AC | 286 ms
82,688 KB |
testcase_24 | AC | 426 ms
89,900 KB |
testcase_25 | AC | 438 ms
89,204 KB |
testcase_26 | AC | 546 ms
94,380 KB |
testcase_27 | AC | 607 ms
98,396 KB |
testcase_28 | AC | 627 ms
99,620 KB |
testcase_29 | AC | 634 ms
99,596 KB |
testcase_30 | AC | 571 ms
100,308 KB |
testcase_31 | AC | 546 ms
98,764 KB |
testcase_32 | AC | 196 ms
77,508 KB |
testcase_33 | AC | 131 ms
76,288 KB |
testcase_34 | AC | 124 ms
76,032 KB |
testcase_35 | AC | 175 ms
78,452 KB |
testcase_36 | AC | 164 ms
77,444 KB |
testcase_37 | AC | 158 ms
77,696 KB |
testcase_38 | AC | 245 ms
79,676 KB |
testcase_39 | AC | 172 ms
77,568 KB |
testcase_40 | AC | 179 ms
77,932 KB |
testcase_41 | AC | 300 ms
86,152 KB |
testcase_42 | AC | 225 ms
79,360 KB |
testcase_43 | AC | 174 ms
78,276 KB |
testcase_44 | AC | 268 ms
80,000 KB |
testcase_45 | AC | 159 ms
78,348 KB |
testcase_46 | AC | 390 ms
88,960 KB |
ソースコード
import heapq as hq import sys input = sys.stdin.readline INF = 10 ** 18 """ Union-Find from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.py ref : https://algo-logic.info/union-find-tree/ """ class UnionFind(): #Uni = UnionFind(n) のようにする def __init__(self, n): self.n = n self.parents = [-1] * n #xの親(親がいないときは自身の番号を返す) def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] #xとyを関係付ける def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x # if x > y: # よりrootのインデックスが小さい方が親 # x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x #xとyが同じ組に属するかどうか def same(self, x, y): return self.find(x) == self.find(y) #xが属する組の大きさ def size(self, x): return -self.parents[self.find(x)] #xが属する組の全要素をリストとして取得 def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] #Union-Find木の根に当たる要素全てをリストとして取得 def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] #Union-Find木の根とそれに属する組の全要素 def all_group_members(self): return {r: self.members(r) for r in self.roots()} N,M,K = map(int, input().split()) if not(2 <= N <= 200 and N - 1 <= M <= N * (N - 1) // 2 and 1 <= K <= min(5, M)): exit(1) R = [i - 1 for i in map(int, input().split())] S = set(R) if any(not(1 <= i + 1 <= M) for i in S): exit(1) if not(len(S) == len(R) == K): exit(1) edge = [[] for _ in [0] * M] route = [[] for _ in [0] * N] abst = set([]) uni = UnionFind(N) for i in range(M): a,b,c = map(int, input().split()) if((a,b) in abst): exit(1) abst.add((a,b)); abst.add((b,a)) if not(1 <= a <= N and 1 <= b <= N and a != b and 1 <= c <= 10 ** 4): exit(1) a -= 1; b -= 1 uni.union(a,b) route[a].append((b,c)) route[b].append((a,c)) if(i in S): edge[i] = [a,b,c] if not(uni.size(0) == N): exit(1) def dijkstra(s,route): que = [(0,s)] dist = [INF] * N while(que): d,v = hq.heappop(que) if(dist[v] < INF): continue dist[v] = d for nv,nd in route[v]: hq.heappush(que,(d + nd, nv)) return dist dist = [[] for _ in [0] * N] T = set([0,N - 1]) for i in S: T.add(edge[i][0]) T.add(edge[i][1]) for i in T: dist[i] = dijkstra(i,route) dp = [[[INF] * 2 for _ in [0] * K] for _ in [0] * (1 << K)] for i in range(K): for j in range(2): dp[1 << i][i][j] = dist[0][edge[R[i]][j ^ 1]] + edge[R[i]][2] for bit in range(1, 1 << K): for s in range(K): if not(bit & (1 << s)): continue for t in range(K): if(bit & (1 << t)): continue b = bit | (1 << t); c = edge[R[t]][2] for x in range(2): for y in range(2): p = edge[R[s]][x]; q = edge[R[t]][y ^ 1] d = dp[bit][s][x] + dist[p][q] + c if(dp[b][t][y] > d): dp[b][t][y] = d ans = INF for i in range(K): for j in range(2): d = dp[-1][i][j] + dist[edge[R[i]][j]][N - 1] if(ans > d): ans = d print(ans)