結果

問題 No.1283 Extra Fee
ユーザー ygd.ygd.
提出日時 2020-11-07 23:29:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,597 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 205,092 KB
最終ジャッジ日時 2023-09-29 21:01:27
合計ジャッジ時間 21,021 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,340 KB
testcase_01 AC 74 ms
70,956 KB
testcase_02 AC 74 ms
71,444 KB
testcase_03 AC 75 ms
71,328 KB
testcase_04 AC 74 ms
71,252 KB
testcase_05 AC 75 ms
71,228 KB
testcase_06 AC 78 ms
71,296 KB
testcase_07 AC 75 ms
71,272 KB
testcase_08 AC 78 ms
71,104 KB
testcase_09 AC 77 ms
71,644 KB
testcase_10 AC 77 ms
71,208 KB
testcase_11 AC 286 ms
85,176 KB
testcase_12 AC 300 ms
85,448 KB
testcase_13 AC 219 ms
82,676 KB
testcase_14 AC 421 ms
98,004 KB
testcase_15 AC 555 ms
108,352 KB
testcase_16 AC 239 ms
84,652 KB
testcase_17 TLE -
testcase_18 AC 1,650 ms
193,224 KB
testcase_19 AC 1,722 ms
202,376 KB
testcase_20 AC 1,614 ms
191,096 KB
testcase_21 AC 1,557 ms
192,228 KB
testcase_22 AC 1,449 ms
178,952 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 1,822 ms
204,516 KB
testcase_26 AC 1,857 ms
204,660 KB
testcase_27 AC 1,861 ms
204,480 KB
testcase_28 AC 1,929 ms
205,092 KB
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop, heappush
N,M = map(int,input().split()); INF = float("inf")
dic = {}
for i in range(M):
  h,w,c = map(int,input().split())
  h-=1;w-=1
  dic[h*N+w] = c
#print(dic)  
G = [[] for _ in range(N*N)]
dxdy = [(0,1),(0,-1),(1,0),(-1,0)]
for i in range(N):
  for j in range(N):
    now = i*N + j
    for k in range(4):
      ni = i+dxdy[k][0]
      nj = j+dxdy[k][1]
      if 0<=ni<N and 0<=nj<N:
        nxt = ni*N+nj
        if nxt in dic:
          G[now].append((1+dic[nxt],nxt))
        else:
          G[now].append((1,nxt))
#print(G)

def dijkstra_heap2(s,G):
  #S:start, V: node, E: Edge, G: Graph
  V = len(G)
  d = [[INF,INF] for _ in range(V)] #d[i][j];j=0権利を行使していないj=1どこかで権利を行使済み
  d[s][0] = 0; d[s][1] = 0
  PQ = []
  heappush(PQ,(0,s,0)) #コスト0,初期点s,権利行使していない
  
  while PQ:
    c,v,flag = heappop(PQ)
    if flag == 0 and d[v][0] < c:
      continue
    if flag == 1 and d[v][1] < c:
      continue
    d[v][flag] = c
    for cost,u in G[v]:
      if flag == 0:
        if d[u][0] <= cost + d[v][0]:
          pass
        else:
          d[u][0] = cost + d[v][0]
          heappush(PQ,(d[u][0], u, 0))
        if d[u][1] <= d[v][0]:
          pass
        else:
          d[u][1] = 1 + d[v][0]
          heappush(PQ,(d[u][1], u, 1))
      else: #flag == 1
        if d[u][1] <= cost + d[v][1]:
          pass
        else:
          d[u][1] = cost + d[v][1]
          heappush(PQ,(d[u][1], u, 1))        
  
  return d

D = dijkstra_heap2(0,G)
#print(D)
ans = min(D[-1])

print(ans)
0