結果

問題 No.2565 はじめてのおつかい
ユーザー 👑 timitimi
提出日時 2023-12-02 15:14:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 626 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 497 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 95,232 KB
最終ジャッジ日時 2023-12-02 15:14:23
合計ジャッジ時間 19,283 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 261 ms
95,232 KB
testcase_04 AC 190 ms
95,232 KB
testcase_05 AC 39 ms
53,460 KB
testcase_06 AC 417 ms
85,528 KB
testcase_07 AC 267 ms
81,032 KB
testcase_08 AC 350 ms
83,072 KB
testcase_09 AC 392 ms
83,964 KB
testcase_10 AC 378 ms
83,904 KB
testcase_11 AC 626 ms
92,768 KB
testcase_12 AC 176 ms
79,220 KB
testcase_13 AC 381 ms
82,508 KB
testcase_14 AC 487 ms
87,148 KB
testcase_15 AC 505 ms
87,888 KB
testcase_16 AC 153 ms
86,848 KB
testcase_17 AC 92 ms
77,044 KB
testcase_18 AC 98 ms
79,140 KB
testcase_19 AC 473 ms
88,700 KB
testcase_20 AC 412 ms
87,172 KB
testcase_21 AC 329 ms
85,444 KB
testcase_22 AC 214 ms
82,044 KB
testcase_23 AC 311 ms
83,304 KB
testcase_24 AC 121 ms
77,812 KB
testcase_25 AC 544 ms
88,852 KB
testcase_26 AC 296 ms
82,616 KB
testcase_27 AC 279 ms
85,780 KB
testcase_28 AC 477 ms
88,616 KB
testcase_29 AC 471 ms
89,972 KB
testcase_30 AC 289 ms
86,480 KB
testcase_31 AC 399 ms
87,560 KB
testcase_32 AC 310 ms
82,800 KB
testcase_33 AC 393 ms
87,752 KB
testcase_34 AC 580 ms
89,620 KB
testcase_35 AC 270 ms
87,368 KB
testcase_36 AC 562 ms
89,204 KB
testcase_37 AC 295 ms
82,992 KB
testcase_38 AC 441 ms
87,660 KB
testcase_39 AC 499 ms
86,652 KB
testcase_40 AC 328 ms
88,008 KB
testcase_41 AC 434 ms
89,764 KB
testcase_42 AC 317 ms
83,668 KB
testcase_43 AC 413 ms
86,412 KB
testcase_44 AC 369 ms
82,636 KB
testcase_45 AC 207 ms
79,444 KB
testcase_46 AC 544 ms
88,680 KB
testcase_47 AC 310 ms
81,524 KB
testcase_48 AC 368 ms
83,152 KB
testcase_49 AC 181 ms
78,964 KB
testcase_50 AC 290 ms
82,804 KB
testcase_51 AC 41 ms
53,460 KB
testcase_52 AC 156 ms
79,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0