結果
問題 | No.2739 Time is money |
ユーザー | Butterflv |
提出日時 | 2024-04-23 20:14:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,801 ms / 2,000 ms |
コード長 | 2,213 bytes |
コンパイル時間 | 370 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 203,276 KB |
最終ジャッジ日時 | 2024-11-06 02:30:48 |
合計ジャッジ時間 | 24,889 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
55,108 KB |
testcase_01 | AC | 43 ms
54,504 KB |
testcase_02 | AC | 338 ms
98,632 KB |
testcase_03 | AC | 1,578 ms
182,604 KB |
testcase_04 | AC | 335 ms
97,864 KB |
testcase_05 | AC | 1,221 ms
152,804 KB |
testcase_06 | AC | 1,686 ms
183,020 KB |
testcase_07 | AC | 1,554 ms
196,816 KB |
testcase_08 | AC | 1,714 ms
199,884 KB |
testcase_09 | AC | 1,784 ms
201,836 KB |
testcase_10 | AC | 456 ms
117,272 KB |
testcase_11 | AC | 1,801 ms
201,804 KB |
testcase_12 | AC | 690 ms
183,724 KB |
testcase_13 | AC | 663 ms
183,492 KB |
testcase_14 | AC | 585 ms
170,636 KB |
testcase_15 | AC | 1,016 ms
192,776 KB |
testcase_16 | AC | 1,063 ms
184,428 KB |
testcase_17 | AC | 1,796 ms
203,276 KB |
testcase_18 | AC | 1,738 ms
196,872 KB |
testcase_19 | AC | 1,318 ms
184,580 KB |
ソースコード
# https://note.nkmk.me/python-union-find/ from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] 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 self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N, M, X=map(int, input().split()) INPUT=tuple(tuple(map(int, input().split())) for i in range(M)) uf=UnionFind(N) for elm in INPUT: u, v, _, __=elm u-=1; v-=1 uf.union(u, v) if uf.same(0, N-1)==False: print(-1); exit() del uf edge=[[] for i in range(N)] for elm in INPUT: u, v, C, T=elm u-=1; v-=1 edge[u].append((v, C, T)) edge[v].append((u, C, T)) # 連結 ⇒ 移動可能 import heapq d=[] timeMIN=[None for i in range(N)] heapq.heapify(d) # 各タプルは (時間, - 残りのお金, 頂点) で入れる heapq.heappush(d, (0, 0, 0)) while len(d)>0: t, mon, v=heapq.heappop(d) mon*=(-1) if timeMIN[v]==None: timeMIN[v]=t else: continue for nv, C, T in edge[v]: if timeMIN[nv]!=None: continue nt=t+T nmon=mon-C if nmon<0: nt+=(abs(nmon)+X-1)//X nmon%=X heapq.heappush(d, (nt, -nmon, nv)) print(timeMIN[N-1])