結果
| 問題 |
No.2569 はじめてのおつかいHard
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2023-12-22 00:14:47 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 837 bytes |
| コンパイル時間 | 275 ms |
| コンパイル使用メモリ | 82,460 KB |
| 実行使用メモリ | 168,740 KB |
| 最終ジャッジ日時 | 2024-09-27 11:15:12 |
| 合計ジャッジ時間 | 7,720 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 2 |
| other | AC * 6 TLE * 1 -- * 3 |
ソースコード
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
N,M=map(int,input().split())
E=[[] for i in range(N+1)]
E_INV=[[] for i in range(N+1)]
for i in range(M):
u,v,t=map(int,input().split())
E[u].append((v,t))
E_INV[v].append((u,t))
def distance(x,EDGE):
DIS=[1<<60]*(N+1)
Q=[(x,0)]
DIS[x]=0
while Q:
now,time=heappop(Q)
if DIS[now]!=time:
continue
for to,cost in EDGE[now]:
if DIS[to]>DIS[now]+cost:
DIS[to]=DIS[now]+cost
heappush(Q,(to,DIS[to]))
return DIS
D1=distance(N-1,E)
D2=distance(N-1,E_INV)
D3=distance(N,E)
D4=distance(N,E_INV)
for i in range(1,N-1):
a=D2[i]+D1[N]+D3[i]
b=D4[i]+D3[N-1]+D1[i]
ANS=min(a,b)
if ANS>1<<60:
print(-1)
else:
print(ANS)
titia