結果

問題 No.2565 はじめてのおつかい
ユーザー Basin-BugBasin-Bug
提出日時 2023-12-02 16:18:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 96,128 KB
最終ジャッジ日時 2023-12-02 16:18:51
合計ジャッジ時間 15,878 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 227 ms
94,852 KB
testcase_04 AC 175 ms
94,852 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 349 ms
88,296 KB
testcase_07 AC 241 ms
82,836 KB
testcase_08 AC 310 ms
84,276 KB
testcase_09 AC 294 ms
86,756 KB
testcase_10 AC 332 ms
85,360 KB
testcase_11 AC 442 ms
96,128 KB
testcase_12 AC 178 ms
80,388 KB
testcase_13 AC 322 ms
85,076 KB
testcase_14 AC 385 ms
91,432 KB
testcase_15 AC 400 ms
90,868 KB
testcase_16 AC 136 ms
84,780 KB
testcase_17 AC 88 ms
76,552 KB
testcase_18 AC 95 ms
78,764 KB
testcase_19 AC 373 ms
93,712 KB
testcase_20 AC 344 ms
91,552 KB
testcase_21 AC 253 ms
90,440 KB
testcase_22 AC 227 ms
84,916 KB
testcase_23 AC 272 ms
86,752 KB
testcase_24 AC 128 ms
78,688 KB
testcase_25 AC 415 ms
93,092 KB
testcase_26 AC 259 ms
85,836 KB
testcase_27 AC 265 ms
91,036 KB
testcase_28 AC 353 ms
92,836 KB
testcase_29 AC 341 ms
95,824 KB
testcase_30 AC 239 ms
91,124 KB
testcase_31 AC 323 ms
91,844 KB
testcase_32 AC 267 ms
85,428 KB
testcase_33 AC 366 ms
92,308 KB
testcase_34 AC 468 ms
92,608 KB
testcase_35 AC 239 ms
92,600 KB
testcase_36 AC 407 ms
94,432 KB
testcase_37 AC 258 ms
86,196 KB
testcase_38 AC 360 ms
91,128 KB
testcase_39 AC 359 ms
90,040 KB
testcase_40 AC 264 ms
93,220 KB
testcase_41 AC 378 ms
95,916 KB
testcase_42 AC 267 ms
85,744 KB
testcase_43 AC 329 ms
89,764 KB
testcase_44 AC 299 ms
84,708 KB
testcase_45 AC 218 ms
81,516 KB
testcase_46 AC 390 ms
91,384 KB
testcase_47 AC 253 ms
85,388 KB
testcase_48 AC 302 ms
85,192 KB
testcase_49 AC 175 ms
80,116 KB
testcase_50 AC 280 ms
85,960 KB
testcase_51 AC 37 ms
53,460 KB
testcase_52 AC 155 ms
84,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

# 入力
N, M = map(int, input().split())
A, B = [ None ] * M, [ None ] * M
for i in range(M):
	A[i], B[i] = map(int, input().split())

# 隣接リストの作成 → グラフの辺を追加
G = [ list() for i in range(N + 1) ]
for i in range(M):
	G[A[i]].append((B[i], 1))

def dijkstra(beg):
  # ダイクストラ法:配列の初期化など
  dist = [ 10 ** 19 ] * (N + 1)
  used = [ False ] * (N + 1)
  Q = list()
  dist[beg] = 0
  heapq.heappush(Q, (0, beg))

  # ダイクストラ法:優先度付きキューの更新
  while len(Q) >= 1:
    pos = heapq.heappop(Q)[1]
    if used[pos] == True:
      continue
    used[pos] = True
    for i in G[pos]:
      to = i[0]
      cost = dist[pos] + i[1]
      if dist[to] > cost:
        dist[to] = cost
        heapq.heappush(Q, (dist[to], to))

  return dist

dist1 = dijkstra(1)
dist2 = dijkstra(N - 1)
dist3 = dijkstra(N)

if min(dist1[N - 1] + dist2[N] + dist3[1], dist1[N] + dist3[N - 1] + dist2[1]) < 10 ** 19:
  print(min(dist1[N - 1] + dist2[N] + dist3[1], dist1[N] + dist3[N - 1] + dist2[1]))
else:
  print(-1)
0