結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー flygonflygon
提出日時 2022-10-14 22:32:39
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,907 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 94,256 KB
最終ジャッジ日時 2023-09-08 22:41:14
合計ジャッジ時間 6,445 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
75,684 KB
testcase_01 AC 75 ms
71,252 KB
testcase_02 AC 74 ms
71,016 KB
testcase_03 AC 74 ms
71,120 KB
testcase_04 AC 261 ms
91,996 KB
testcase_05 AC 228 ms
86,924 KB
testcase_06 AC 124 ms
79,536 KB
testcase_07 AC 134 ms
81,500 KB
testcase_08 AC 235 ms
94,256 KB
testcase_09 AC 268 ms
89,772 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m = map(int,input().split())
h = list(map(int,input().split()))
graph = [[] for i in range(n+1)]
for i in range(m):
  a,b = map(int,input().split())
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)

for i in range(n):
  graph[i].sort()


import heapq
#ダイクストラ法
INF = 10**15
dist = [[INF,INF] for i in range(n*1)]
que = [(0,0,0), (0,1,0)]
dist[0][0] = 0
dist[0][1] = 0
while que:
  c,ud,crr = heapq.heappop(que)
  if c > dist[crr][ud]: continue
  for nxt in graph[crr][::-1]:
    if crr > nxt: break
    if ud == 0:
      if h[nxt] < h[crr]: 
        if dist[crr][0] < dist[nxt][1]:
          dist[nxt][1] = dist[crr][0] 
          heapq.heappush(que,(dist[nxt][1],1,nxt))
      else:
        if dist[crr][0] + (h[crr]-h[nxt]) < dist[nxt][1]:
          dist[nxt][1] = dist[crr][0] +  (h[crr]-h[nxt]) 
          heapq.heappush(que,(dist[nxt][1],1,nxt))
    else:
      if h[nxt] > h[crr]: continue
      if dist[crr][1] < dist[nxt][0]:
        dist[nxt][0] = dist[crr][1] 
        heapq.heappush(que,(dist[nxt][0],0,nxt))
ans = min(dist[n-1])
print(-1) if ans == INF else print(-ans)
dist = [[INF,INF] for i in range(n*1)]
que = [(0,0,n-1), (0,1,n-1)]
dist[n-1][0] = 0
dist[n-1][1] = 0
while que:
  c,ud,crr = heapq.heappop(que)
  if c > dist[crr][ud]: continue
  for nxt in graph[crr]:
    if crr < nxt: break
    if ud == 0:
      if h[nxt] < h[crr]: 
        if dist[crr][0] < dist[nxt][1]:
          dist[nxt][1] = dist[crr][0] 
          heapq.heappush(que,(dist[nxt][1],1,nxt))
      else:
        if dist[crr][0] + (h[crr]-h[nxt]) < dist[nxt][1]:
          dist[nxt][1] = dist[crr][0] +  (h[crr]-h[nxt]) 
          heapq.heappush(que,(dist[nxt][1],1,nxt))
    else:
      if h[nxt] > h[crr]: continue
      if dist[crr][1] < dist[nxt][0]:
        dist[nxt][0] = dist[crr][1] 
        heapq.heappush(que,(dist[nxt][0],0,nxt))
ans = min(dist[0])
print(-1) if ans == INF else print(-ans)

0