結果

問題 No.2712 Play more!
コンテスト
ユーザー timi
提出日時 2024-04-06 13:12:38
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,013 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 192 ms
コンパイル使用メモリ 85,404 KB
実行使用メモリ 83,424 KB
最終ジャッジ日時 2026-04-17 03:52:11
合計ジャッジ時間 4,394 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N,M=map(int,input().split())
A=list(map(int, input().split()))
n=N
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=[]
D=[[]for _ in range(N)];C=[]
for _ in range(M):
  x,y,z=map(int,input().split())
  x-=1;y-=1
  C.append((x,y,z))
  D[y].append(x)
  #g.append([x, y, -z])
  
from collections import deque
d=deque()
V=[-1]*N 
V[N-1]=1
d.append(N-1)
while d:
  now=d.popleft()
  for nex in D[now]:
    if V[nex]==-1:
      V[nex]=1
      d.append(nex)
      
for x,y,z in C:
  if V[x]+V[y]==2:
    g.append((x,y,-A[y]+z))
#print(g,D)
ans=bellman_ford(0)
#print(ans)
if ans==-1:
  print('inf')
else:
  print(A[0]-ans[-1])
0