結果
問題 | No.1814 Uribo Road (Easy) |
ユーザー | MasKoaTS |
提出日時 | 2022-01-12 22:09:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 613 ms / 2,000 ms |
コード長 | 3,272 bytes |
コンパイル時間 | 378 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 99,912 KB |
最終ジャッジ日時 | 2024-05-01 17:33:42 |
合計ジャッジ時間 | 12,054 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
53,632 KB |
testcase_01 | AC | 48 ms
54,528 KB |
testcase_02 | AC | 46 ms
53,760 KB |
testcase_03 | AC | 45 ms
53,120 KB |
testcase_04 | AC | 46 ms
52,992 KB |
testcase_05 | AC | 45 ms
53,376 KB |
testcase_06 | AC | 47 ms
53,760 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,748 KB |
testcase_11 | AC | 149 ms
77,952 KB |
testcase_12 | AC | 183 ms
77,824 KB |
testcase_13 | AC | 170 ms
78,208 KB |
testcase_14 | AC | 214 ms
78,360 KB |
testcase_15 | AC | 113 ms
76,160 KB |
testcase_16 | AC | 110 ms
76,032 KB |
testcase_17 | AC | 136 ms
76,416 KB |
testcase_18 | AC | 100 ms
76,160 KB |
testcase_19 | AC | 82 ms
70,656 KB |
testcase_20 | AC | 259 ms
82,048 KB |
testcase_21 | AC | 172 ms
78,304 KB |
testcase_22 | AC | 372 ms
88,640 KB |
testcase_23 | AC | 284 ms
82,432 KB |
testcase_24 | AC | 420 ms
89,904 KB |
testcase_25 | AC | 427 ms
89,456 KB |
testcase_26 | AC | 519 ms
94,896 KB |
testcase_27 | AC | 593 ms
99,424 KB |
testcase_28 | AC | 595 ms
99,372 KB |
testcase_29 | AC | 613 ms
99,216 KB |
testcase_30 | AC | 561 ms
99,912 KB |
testcase_31 | AC | 525 ms
98,428 KB |
testcase_32 | AC | 192 ms
78,268 KB |
testcase_33 | AC | 130 ms
76,416 KB |
testcase_34 | AC | 124 ms
76,544 KB |
testcase_35 | AC | 171 ms
78,324 KB |
testcase_36 | AC | 161 ms
77,572 KB |
testcase_37 | AC | 157 ms
77,824 KB |
testcase_38 | AC | 242 ms
79,404 KB |
testcase_39 | AC | 171 ms
77,696 KB |
testcase_40 | AC | 180 ms
77,568 KB |
testcase_41 | AC | 296 ms
86,664 KB |
testcase_42 | AC | 216 ms
79,488 KB |
testcase_43 | AC | 171 ms
77,896 KB |
testcase_44 | AC | 260 ms
80,896 KB |
testcase_45 | AC | 158 ms
77,576 KB |
testcase_46 | AC | 384 ms
89,216 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)