結果

問題 No.2712 Play more!
ユーザー hiro1729hiro1729
提出日時 2024-03-31 14:47:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,514 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 79,912 KB
最終ジャッジ日時 2024-03-31 14:47:29
合計ジャッジ時間 7,773 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
68,228 KB
testcase_01 AC 60 ms
68,228 KB
testcase_02 AC 61 ms
68,228 KB
testcase_03 AC 60 ms
68,228 KB
testcase_04 WA -
testcase_05 AC 61 ms
68,228 KB
testcase_06 AC 60 ms
68,228 KB
testcase_07 AC 325 ms
79,400 KB
testcase_08 AC 305 ms
79,400 KB
testcase_09 AC 285 ms
79,400 KB
testcase_10 WA -
testcase_11 AC 213 ms
79,132 KB
testcase_12 AC 305 ms
79,272 KB
testcase_13 AC 191 ms
79,132 KB
testcase_14 AC 275 ms
79,144 KB
testcase_15 AC 567 ms
79,612 KB
testcase_16 AC 156 ms
78,620 KB
testcase_17 AC 115 ms
77,980 KB
testcase_18 AC 144 ms
78,620 KB
testcase_19 AC 124 ms
77,980 KB
testcase_20 AC 225 ms
79,132 KB
testcase_21 AC 180 ms
79,388 KB
testcase_22 AC 352 ms
79,400 KB
testcase_23 AC 121 ms
77,980 KB
testcase_24 AC 166 ms
78,748 KB
testcase_25 AC 111 ms
77,980 KB
testcase_26 AC 154 ms
78,876 KB
testcase_27 AC 141 ms
78,888 KB
testcase_28 AC 145 ms
78,120 KB
testcase_29 AC 171 ms
79,260 KB
testcase_30 AC 360 ms
79,400 KB
testcase_31 AC 482 ms
79,912 KB
testcase_32 AC 379 ms
78,888 KB
testcase_33 AC 76 ms
72,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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(max(0, dist[N - 1] + A[0]) if dist[N - 1] > -inf else "inf")
0