結果
問題 | No.2565 はじめてのおつかい |
ユーザー |
![]() |
提出日時 | 2023-12-02 15:14:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 618 ms / 2,000 ms |
コード長 | 845 bytes |
コンパイル時間 | 632 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 95,744 KB |
最終ジャッジ日時 | 2024-09-26 18:18:57 |
合計ジャッジ時間 | 19,561 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
import heapq def dijkstra(s): hq=[(0,s,0)] ans=[] heapq.heapify(hq) # リストを優先度付きキューに変換 cost=[10**20]*N # 行ったことのないところはinf cost[s]=0 # 開始地点は0 while hq: c,v,pre=heapq.heappop(hq) if c>cost[v]: # コストが現在のコストよりも高ければスルー v:now u:nex continue for d, u in E[v]: tmp=d+cost[v] if tmp<cost[u]: cost[u]=tmp heapq.heappush(hq,(tmp,u,v)) return cost N,M=map(int,input().split()) E=[[] for _ in range(N)] D={} for i in range(M): a,b=map(int,input().split()) a-=1;b-=1 E[a].append((1,b)) A=dijkstra(0) B=dijkstra(N-2) C=dijkstra(N-1) D=[] ansa=A[N-2]+B[N-1]+C[0] ansb=A[N-1]+C[N-2]+B[0] ans=min(ansa,ansb,10**20) if ans==10**20: print(-1) else: print(ans)