結果

問題 No.2565 はじめてのおつかい
ユーザー navel_tosnavel_tos
提出日時 2023-12-02 15:18:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 1,468 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 158,964 KB
最終ジャッジ日時 2023-12-02 15:19:05
合計ジャッジ時間 12,990 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 534 ms
158,960 KB
testcase_04 AC 266 ms
158,964 KB
testcase_05 AC 35 ms
53,460 KB
testcase_06 AC 206 ms
87,720 KB
testcase_07 AC 175 ms
83,728 KB
testcase_08 AC 173 ms
86,436 KB
testcase_09 AC 186 ms
87,568 KB
testcase_10 AC 178 ms
86,576 KB
testcase_11 AC 379 ms
113,648 KB
testcase_12 AC 112 ms
77,672 KB
testcase_13 AC 197 ms
84,240 KB
testcase_14 AC 277 ms
100,040 KB
testcase_15 AC 246 ms
97,288 KB
testcase_16 AC 214 ms
97,760 KB
testcase_17 AC 84 ms
77,840 KB
testcase_18 AC 98 ms
83,012 KB
testcase_19 AC 306 ms
101,148 KB
testcase_20 AC 245 ms
95,620 KB
testcase_21 AC 237 ms
96,268 KB
testcase_22 AC 169 ms
90,268 KB
testcase_23 AC 196 ms
92,816 KB
testcase_24 AC 105 ms
77,728 KB
testcase_25 AC 276 ms
96,920 KB
testcase_26 AC 177 ms
89,580 KB
testcase_27 AC 256 ms
100,924 KB
testcase_28 AC 303 ms
104,968 KB
testcase_29 AC 393 ms
110,700 KB
testcase_30 AC 285 ms
101,488 KB
testcase_31 AC 289 ms
105,752 KB
testcase_32 AC 195 ms
90,360 KB
testcase_33 AC 317 ms
102,744 KB
testcase_34 AC 316 ms
105,296 KB
testcase_35 AC 235 ms
98,004 KB
testcase_36 AC 300 ms
102,740 KB
testcase_37 AC 174 ms
90,116 KB
testcase_38 AC 249 ms
95,316 KB
testcase_39 AC 228 ms
94,612 KB
testcase_40 AC 321 ms
106,144 KB
testcase_41 AC 339 ms
109,920 KB
testcase_42 AC 213 ms
92,504 KB
testcase_43 AC 258 ms
96,572 KB
testcase_44 AC 178 ms
85,924 KB
testcase_45 AC 134 ms
80,784 KB
testcase_46 AC 288 ms
97,296 KB
testcase_47 AC 157 ms
81,040 KB
testcase_48 AC 193 ms
85,272 KB
testcase_49 AC 111 ms
77,796 KB
testcase_50 AC 169 ms
83,488 KB
testcase_51 AC 32 ms
53,460 KB
testcase_52 AC 115 ms
79,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#緑以下I

N,M = map(int,input().split())

#G[i][x]: xは0なら行き、1なら帰り
G = [[[] for _ in range(2)] for _ in range(N)]
for _ in range(M):
    u,v = map(lambda x:int(x)-1,input().split())
    G[u][0].append(v); G[v][1].append(u)

#町1から町N-1, 町Nの最短経路を調べる
dist = [[10**18]*2 for _ in range(N)]
Q = [(0,0,0),(0,0,1)]  #累計時間, 現在地点, 移動モード
for time,now,x in Q:
    if time > dist[now][x]: continue
    dist[now][x] = time
    for next in G[now][x]:
        if dist[next][x] > time+1:
            dist[next][x] = time+1
            Q.append((time+1, next, x))
del Q

#町N-2→町N-1の移動時間を調べる
go = [10**18]*N
Q = [(0,N-2)]  #累計時間, 現在地点, 移動モード
for time,now in Q:
    if time > go[now]: continue
    go[now] = time
    for next in G[now][0]:
        if go[next] > time+1:
            go[next] = time+1
            Q.append((time+1, next))
del Q
go = go[N-1]

#町N-1→町N-2
back = [10**18]*N
Q = [(0,N-1)]  #累計時間, 現在地点, 移動モード
for time,now in Q:
    if time > back[now]: continue
    back[now] = time
    for next in G[now][0]:
        if back[next] > time+1:
            back[next] = time+1
            Q.append((time+1, next))
del Q
back = back[N-2]

ans = 10**18
#0→N-2→N-1→0
ans = min(ans, dist[N-2][0] + go + dist[N-1][1])
#0→N-1→N-2→0
ans = min(ans, dist[N-1][0] + back + dist[N-2][1])
print(ans if ans<10**18 else -1)
0