結果
| 問題 | No.1565 Union | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-06-26 15:25:39 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,245 ms / 2,000 ms | 
| コード長 | 1,367 bytes | 
| コンパイル時間 | 277 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 78,336 KB | 
| 最終ジャッジ日時 | 2024-09-27 05:50:51 | 
| 合計ジャッジ時間 | 17,046 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
import sys
#sys.setrecursionlimit(10 ** 6)
INF = float('inf')
#10**20に変えるのもあり
MOD = 10**9 + 7
MOD2 = 998244353
def solve():
    def II(): return int(sys.stdin.readline())
    def LI(): return list(map(int, sys.stdin.readline().split()))
    def LC(): return list(input())
    def IC(): return [int(c) for c in input()]
    def MI(): return map(int, sys.stdin.readline().split())
    N,M = MI()
    Graph = [[] for n in range(N + 1)]
    for i in range(M):
        A, B = MI()
        Graph[A].append((B,1))
        Graph[B].append((A,1))
    from heapq import heappush, heappop
    def dijkstra(s, n):  # (始点, ノード数)
        dist = [INF] * n
        hq = [(0, s)]  # (distance, node)
        dist[s] = 0
        seen = [False] * n  # ノードが確定済みかどうか
        while hq:
            v = heappop(hq)[1]  # ノードを pop する
            if seen[v]:
                continue
            seen[v] = True
            for to, cost in Graph[v]:  # ノード v に隣接しているノードに対して
                if seen[to] == False and dist[v] + cost < dist[to]:
                    dist[to] = dist[v] + cost
                    heappush(hq, (dist[to], to))
        return dist
    Dist = dijkstra(1, N+1)
    #print(Dist)
    if(Dist[N] == INF):
        print(-1)
    else:
        print(Dist[N])
    return
solve()
            
            
            
        