結果

問題 No.2712 Play more!
ユーザー hato336
提出日時 2024-03-31 14:07:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 850 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 91,496 KB
最終ジャッジ日時 2024-09-30 19:01:09
合計ジャッジ時間 15,006 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
# O(EV)
def bellman_ford(s):
    d = [float('inf')]*n # 各頂点への最小コスト
    d[s] = 0 # 自身への距離は0
    for i in range(n):
        update = False # 更新が行われたか
        for x, y, z in g:
            if d[y] > d[x] + z:
                d[y] = d[x] + z
                update = True
        if not update:
            break
        # 負閉路が存在
        if i == n - 1:
            return -1
    return d


g = []


n,m = map(int,input().split())
n *= 2
alist = list(map(int,input().split()))
for i in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    g.append((a*2+1,b*2,c))
for i in range(n//2):
    g.append((i*2,i*2+1,-alist[i]))
z = bellman_ford(0)
print('inf' if z == -1 else -z[-1])
0