結果

問題 No.2712 Play more!
ユーザー 👑 rin204rin204
提出日時 2024-04-07 14:38:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,388 ms / 2,000 ms
コード長 1,486 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 77,592 KB
最終ジャッジ日時 2024-04-07 14:38:16
合計ジャッジ時間 13,883 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 726 ms
76,824 KB
testcase_08 AC 633 ms
77,592 KB
testcase_09 AC 737 ms
76,824 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 387 ms
76,676 KB
testcase_12 AC 1,244 ms
77,080 KB
testcase_13 AC 306 ms
76,676 KB
testcase_14 AC 928 ms
76,836 KB
testcase_15 AC 1,388 ms
77,208 KB
testcase_16 AC 193 ms
76,548 KB
testcase_17 AC 103 ms
76,688 KB
testcase_18 AC 261 ms
76,692 KB
testcase_19 AC 140 ms
76,704 KB
testcase_20 AC 419 ms
76,800 KB
testcase_21 AC 294 ms
76,776 KB
testcase_22 AC 1,235 ms
77,080 KB
testcase_23 AC 128 ms
76,520 KB
testcase_24 AC 263 ms
76,816 KB
testcase_25 AC 101 ms
76,804 KB
testcase_26 AC 287 ms
76,564 KB
testcase_27 AC 290 ms
76,836 KB
testcase_28 AC 82 ms
66,340 KB
testcase_29 AC 270 ms
76,816 KB
testcase_30 AC 1,293 ms
76,824 KB
testcase_31 AC 82 ms
76,952 KB
testcase_32 AC 83 ms
76,696 KB
testcase_33 AC 46 ms
61,128 KB
testcase_34 AC 37 ms
53,460 KB
testcase_35 AC 37 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bellmanFord(n, edges, s, inf=1 << 60):
    """
    edges = [(from1, to1, cost1), (from2, to2, cost2), ...)]
    """
    dist = [inf] * n
    dist[s] = 0

    for _ in range(n):
        update = False
        for u, v, d in edges:
            if dist[u] != inf and dist[v] > dist[u] + d:
                dist[v] = dist[u] + d
                update = True

        if not update:
            return dist

    bd = dist[:]
    for _ in range(n):
        update = False
        for u, v, d in edges:
            if dist[u] != inf and dist[v] > dist[u] + d:
                dist[v] = dist[u] + d
                update = True

        if not update:
            return -dist[-1]

    st = []
    used = [False] * n
    for i in range(n):
        if bd[i] != dist[i]:
            dist[i] = None
            st.append(i)
            used[i] = True
    E = [[] for _ in range(n)]
    for u, v, d in edges:
        E[u].append(v)

    while st:
        v = st.pop()
        for u in E[v]:
            if not used[u]:
                used[u] = True
                st.append(u)
                dist[u] = None

    return dist


n, m = map(int, input().split())
A = list(map(int, input().split()))
edges = []
for i, a in enumerate(A):
    edges.append((2 * i, 2 * i + 1, -a))
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((2 * a + 1, 2 * b, c))

ans = bellmanFord(2 * n, edges, 0)[-1]
if ans is None:
    print("inf")
else:
    print(-ans)
0