結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー 高橋ゆに
提出日時 2025-03-05 22:11:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 541 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 607 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 127,924 KB
最終ジャッジ日時 2025-11-01 09:25:41
合計ジャッジ時間 17,556 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import heapq

def main():
    N, M = map(int, input().split())
    graph = [[] for _ in range(N + 1)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        graph[u].append(v)
        graph[v].append(u)
    
    dists0 = [0] * N
    dists1 = [-10**18] * N
    
    K = int(input())
    q0 = []
    for _ in range(K):
        pos, dist = map(int, input().split())
        pos -= 1
        dist += 1
        q0.append((-dist, pos))
        dists0[pos] = dist
    while q0:
        dist, curr = heapq.heappop(q0)
        dist *= -1
        if dists0[curr] < dist:
            continue
        for to in graph[curr]:
            new_dist = dists0[curr] - 1
            if new_dist > dists0[to]:
                dists0[to] = new_dist
                if dists0[to] != 0:
                    heapq.heappush(q0, (-new_dist, to))
    
    q1= deque()
    if dists0[0] ==0:
        q1.append(0)
        dists1[0] = 0
    while q1:
        curr = q1.popleft()
        for to in graph[curr]:
            if dists0[to] == 0 and dists1[to] == -10**18:
                dists1[to] = dists1[curr] + 1
                q1.append(to)
    
    ans = dists1[-1]
    if ans == -10**18:
        print('No')
    else:
        print('Yes')
        print(ans)

if __name__ == '__main__':
    main()
0