結果

問題 No.1565 Union
ユーザー googol_S0googol_S0
提出日時 2021-06-26 13:46:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 880 ms / 2,000 ms
コード長 597 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 112,924 KB
最終ジャッジ日時 2023-12-20 06:45:07
合計ジャッジ時間 12,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,588 KB
testcase_01 AC 47 ms
53,588 KB
testcase_02 AC 50 ms
53,588 KB
testcase_03 AC 48 ms
53,588 KB
testcase_04 AC 42 ms
53,588 KB
testcase_05 AC 42 ms
53,588 KB
testcase_06 AC 48 ms
53,588 KB
testcase_07 AC 55 ms
53,588 KB
testcase_08 AC 44 ms
53,588 KB
testcase_09 AC 48 ms
53,588 KB
testcase_10 AC 300 ms
88,856 KB
testcase_11 AC 453 ms
99,672 KB
testcase_12 AC 467 ms
95,884 KB
testcase_13 AC 223 ms
83,784 KB
testcase_14 AC 546 ms
100,916 KB
testcase_15 AC 880 ms
112,788 KB
testcase_16 AC 458 ms
107,872 KB
testcase_17 AC 774 ms
112,924 KB
testcase_18 AC 810 ms
112,916 KB
testcase_19 AC 787 ms
112,868 KB
testcase_20 AC 274 ms
109,664 KB
testcase_21 AC 309 ms
109,672 KB
testcase_22 AC 288 ms
109,668 KB
testcase_23 AC 298 ms
109,664 KB
testcase_24 AC 294 ms
109,668 KB
testcase_25 AC 395 ms
110,184 KB
testcase_26 AC 353 ms
110,052 KB
testcase_27 AC 379 ms
110,056 KB
testcase_28 AC 366 ms
110,308 KB
testcase_29 AC 365 ms
110,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
from heapq import *
G=[[] for i in range(N)]
D=[0]*N
INF=10**17
def ijk(s):
  global D,INF,G
  for i in range(len(G)):
    D[i]=INF
  D[s]=0
  Q=[]
  heapify(Q)
  heappush(Q,(0,s))
  p,v,e=0,0,0
  while len(Q):
    p=heappop(Q)
    v=p[1]
    if D[v]<p[0]:
      continue
    for i in range(len(G[v])):
      e=G[v][i]
      if D[e[0]]>D[v]+e[1]:
        D[e[0]]=D[v]+e[1]
        heappush(Q,(D[e[0]],e[0]))

for i in range(M):
  a,b=map(int,input().split())
  a-=1
  b-=1
  G[a].append((b,1))
  G[b].append((a,1))
ijk(0)
if D[-1]<INF:
  print(D[-1])
else:
  print(-1)
0