結果
| 問題 |
No.3263 違法な散歩道
|
| コンテスト | |
| ユーザー |
bolero
|
| 提出日時 | 2025-08-14 16:30:36 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 297 ms / 2,000 ms |
| コード長 | 1,243 bytes |
| コンパイル時間 | 406 ms |
| コンパイル使用メモリ | 82,688 KB |
| 実行使用メモリ | 118,144 KB |
| 最終ジャッジ日時 | 2025-08-14 16:30:43 |
| 合計ジャッジ時間 | 7,016 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
# 想定解法(1112134) をChatGPTで変換した
import sys
from collections import deque
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
N = next(it)
M = next(it)
edges = [[] for _ in range(N)]
for _ in range(M):
u = next(it) - 1
v = next(it) - 1
edges[u].append(v)
edges[v].append(u)
K = next(it)
is_iwai = [False] * N
for _ in range(K):
A = next(it) - 1
is_iwai[A] = True
INF = 10**18
# dists[i][j] := 岩井星人に連続でj回会ったうえで頂点iに到達するまでの最短距離
dists = [[INF] * 5 for _ in range(N)]
q = deque()
q.append((0, 0))
dists[0][0] = 0
while q:
now, iwai_cnt = q.popleft()
nowd = dists[now][iwai_cnt]
for nxt in edges[now]:
next_iwai_cnt = iwai_cnt + 1 if is_iwai[nxt] else 0
if next_iwai_cnt >= 5:
continue
if dists[nxt][next_iwai_cnt] <= nowd + 1:
continue
dists[nxt][next_iwai_cnt] = nowd + 1
q.append((nxt, next_iwai_cnt))
ans = dists[N - 1][0]
print(-1 if ans == INF else ans)
if __name__ == "__main__":
main()
bolero