結果
問題 | No.2712 Play more! |
ユーザー |
![]() |
提出日時 | 2024-04-06 13:12:38 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 420 ms / 2,000 ms |
コード長 | 1,013 bytes |
コンパイル時間 | 213 ms |
コンパイル使用メモリ | 82,540 KB |
実行使用メモリ | 78,524 KB |
最終ジャッジ日時 | 2024-10-01 03:49:11 |
合計ジャッジ時間 | 5,739 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
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])