結果
問題 | No.2712 Play more! |
ユーザー | hiro1729 |
提出日時 | 2024-03-31 14:43:52 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,506 bytes |
コンパイル時間 | 237 ms |
コンパイル使用メモリ | 82,212 KB |
実行使用メモリ | 80,424 KB |
最終ジャッジ日時 | 2024-09-30 19:52:22 |
合計ジャッジ時間 | 8,100 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 65 ms
67,968 KB |
testcase_01 | AC | 67 ms
69,312 KB |
testcase_02 | AC | 66 ms
68,928 KB |
testcase_03 | AC | 66 ms
67,920 KB |
testcase_04 | WA | - |
testcase_05 | AC | 68 ms
68,440 KB |
testcase_06 | AC | 64 ms
68,528 KB |
testcase_07 | AC | 325 ms
80,028 KB |
testcase_08 | AC | 303 ms
80,084 KB |
testcase_09 | AC | 291 ms
79,992 KB |
testcase_10 | WA | - |
testcase_11 | AC | 205 ms
79,672 KB |
testcase_12 | AC | 313 ms
79,540 KB |
testcase_13 | AC | 197 ms
79,524 KB |
testcase_14 | AC | 251 ms
79,424 KB |
testcase_15 | AC | 556 ms
79,832 KB |
testcase_16 | AC | 160 ms
78,976 KB |
testcase_17 | AC | 116 ms
78,796 KB |
testcase_18 | AC | 146 ms
78,956 KB |
testcase_19 | AC | 126 ms
78,140 KB |
testcase_20 | AC | 227 ms
79,612 KB |
testcase_21 | AC | 187 ms
79,864 KB |
testcase_22 | AC | 357 ms
80,008 KB |
testcase_23 | AC | 125 ms
78,256 KB |
testcase_24 | AC | 167 ms
79,020 KB |
testcase_25 | AC | 119 ms
78,152 KB |
testcase_26 | AC | 163 ms
79,360 KB |
testcase_27 | AC | 142 ms
79,644 KB |
testcase_28 | AC | 151 ms
78,408 KB |
testcase_29 | AC | 168 ms
79,592 KB |
testcase_30 | AC | 347 ms
79,544 KB |
testcase_31 | AC | 468 ms
80,424 KB |
testcase_32 | AC | 372 ms
79,312 KB |
testcase_33 | AC | 78 ms
73,028 KB |
testcase_34 | AC | 65 ms
68,924 KB |
testcase_35 | AC | 66 ms
68,704 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) s = [] for i in range(N): if uf.same(0, i): s.append(i) ok = set(s) es = [] for i, j, c in g: if i in ok and j in ok: es.append((i, j, c)) inf = 10 ** 20 dist = [-inf] * N dist[0] = 0 for i in range(N + 1): for u, v, c in es: if dist[u] != -inf and dist[v] < dist[u] + A[v] - c: dist[v] = dist[u] + A[v] - c for u, v, c in es: if uf.same(0, u) and uf.same(N - 1, 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")