結果
問題 | No.2712 Play more! |
ユーザー | hiro1729 |
提出日時 | 2024-03-31 14:36:28 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,337 bytes |
コンパイル時間 | 191 ms |
コンパイル使用メモリ | 82,796 KB |
実行使用メモリ | 79,952 KB |
最終ジャッジ日時 | 2024-09-30 19:45:02 |
合計ジャッジ時間 | 7,936 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
67,896 KB |
testcase_01 | AC | 69 ms
68,800 KB |
testcase_02 | AC | 64 ms
68,768 KB |
testcase_03 | AC | 63 ms
67,936 KB |
testcase_04 | WA | - |
testcase_05 | AC | 63 ms
68,116 KB |
testcase_06 | AC | 62 ms
68,520 KB |
testcase_07 | AC | 306 ms
79,300 KB |
testcase_08 | AC | 320 ms
79,684 KB |
testcase_09 | AC | 316 ms
79,424 KB |
testcase_10 | WA | - |
testcase_11 | AC | 218 ms
79,264 KB |
testcase_12 | AC | 331 ms
79,428 KB |
testcase_13 | AC | 204 ms
79,076 KB |
testcase_14 | AC | 283 ms
79,408 KB |
testcase_15 | AC | 567 ms
79,952 KB |
testcase_16 | AC | 135 ms
79,320 KB |
testcase_17 | AC | 109 ms
79,016 KB |
testcase_18 | AC | 127 ms
78,516 KB |
testcase_19 | AC | 115 ms
78,800 KB |
testcase_20 | AC | 203 ms
79,464 KB |
testcase_21 | AC | 163 ms
79,616 KB |
testcase_22 | AC | 297 ms
79,492 KB |
testcase_23 | AC | 124 ms
79,052 KB |
testcase_24 | AC | 163 ms
78,644 KB |
testcase_25 | AC | 112 ms
78,440 KB |
testcase_26 | AC | 146 ms
78,968 KB |
testcase_27 | AC | 137 ms
78,700 KB |
testcase_28 | AC | 128 ms
78,316 KB |
testcase_29 | AC | 155 ms
78,360 KB |
testcase_30 | AC | 306 ms
79,780 KB |
testcase_31 | AC | 451 ms
79,484 KB |
testcase_32 | AC | 277 ms
79,620 KB |
testcase_33 | AC | 81 ms
72,852 KB |
testcase_34 | AC | 63 ms
68,376 KB |
testcase_35 | AC | 64 ms
68,648 KB |
ソースコード
import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= 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: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) from heapq import * N, M = map(int, input().split()) A = list(map(int, input().split())) g = [] uf = DSU(N) for _ in range(M): a, b, c = map(int, input().split()) a -= 1 b -= 1 g.append((a, b, c)) uf.merge(a, b) inf = 10 ** 20 dist = [-inf] * N dist[0] = 0 for i in range(N + 1): for u, v, c in g: if dist[u] != -inf and dist[v] < dist[u] + A[v] - c: dist[v] = dist[u] + A[v] - c for u, v, c in g: if uf.same(0, u) and dist[u] != -inf and dist[v] < dist[u] + A[v] - c: exit(print("inf")) print(dist[N - 1] + A[0] if dist[N - 1] > -inf else "inf")