結果

問題 No.2712 Play more!
ユーザー ra5anchorra5anchor
提出日時 2024-04-13 10:08:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 486 ms / 2,000 ms
コード長 1,647 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 82,504 KB
実行使用メモリ 78,396 KB
最終ジャッジ日時 2024-04-13 10:08:51
合計ジャッジ時間 7,603 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,792 KB
testcase_01 AC 43 ms
55,064 KB
testcase_02 AC 42 ms
54,016 KB
testcase_03 AC 45 ms
53,852 KB
testcase_04 AC 42 ms
54,584 KB
testcase_05 AC 53 ms
55,276 KB
testcase_06 AC 55 ms
53,912 KB
testcase_07 AC 302 ms
78,004 KB
testcase_08 AC 229 ms
77,956 KB
testcase_09 AC 231 ms
78,028 KB
testcase_10 AC 44 ms
54,796 KB
testcase_11 AC 183 ms
77,744 KB
testcase_12 AC 300 ms
78,068 KB
testcase_13 AC 209 ms
77,332 KB
testcase_14 AC 209 ms
77,876 KB
testcase_15 AC 486 ms
78,396 KB
testcase_16 AC 125 ms
77,952 KB
testcase_17 AC 90 ms
77,256 KB
testcase_18 AC 137 ms
77,300 KB
testcase_19 AC 140 ms
77,096 KB
testcase_20 AC 216 ms
77,764 KB
testcase_21 AC 142 ms
77,660 KB
testcase_22 AC 317 ms
78,244 KB
testcase_23 AC 98 ms
77,220 KB
testcase_24 AC 139 ms
77,272 KB
testcase_25 AC 91 ms
77,372 KB
testcase_26 AC 149 ms
77,232 KB
testcase_27 AC 197 ms
77,572 KB
testcase_28 AC 72 ms
70,012 KB
testcase_29 AC 138 ms
77,584 KB
testcase_30 AC 295 ms
77,904 KB
testcase_31 AC 88 ms
77,812 KB
testcase_32 AC 92 ms
77,240 KB
testcase_33 AC 50 ms
57,312 KB
testcase_34 AC 43 ms
54,300 KB
testcase_35 AC 44 ms
54,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
A = list(map(int, input().split()))

G = [[] for _ in range(N)]
Grev = [[] for _ in range(N)]
E = []
for i in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    c *= -1
    c += A[b]
    E.append((a, b, c))
    Grev[b].append(a)

def BF(start):
    # Bellman-Ford
    score = [-float('inf')] * N # スタート地点からの最小距離
    score[start] = A[start]

    for i in range(N):
        update = False # 更新が行われたか
        for u, v, weight in E:
            if score[v] < score[u] + weight:
                score[v] = score[u] + weight
                update = True
        if not update:
            break
        # 負閉路が存在するときは負閉路内の頂点リストを返す
        if i == N - 1:
            st = set()
            for u, v, weight in E:
                if score[v] < score[u] + weight:
                    score[v] = score[u] + weight
                    st.add(v)
            return 1, score, st
    return 0, score, 0

from collections import deque
# print(E)
state, score, st = BF(0) # ノード0から各地点の最短距離
if state == 0:
    print(score[N-1])
else:
    sctmp = score[N-1]
    goal = st
    que = deque()
    visited = [False]*N
    stt = N-1
    visited[stt] = True
    que.append(stt)
    # print(goal)
    while que:
        now = que.popleft()
        for to in Grev[now]:
            if visited[to] == False:
                if to in goal:
                    print('inf')
                    exit()
                visited[to] = True
                que.append(to)
    print(sctmp)
0