結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
rlangevin
|
| 提出日時 | 2023-10-11 11:49:15 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,915 ms / 2,000 ms |
| コード長 | 1,856 bytes |
| コンパイル時間 | 920 ms |
| コンパイル使用メモリ | 82,212 KB |
| 実行使用メモリ | 340,248 KB |
| 最終ジャッジ日時 | 2024-09-13 10:35:04 |
| 合計ジャッジ時間 | 29,328 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
import sys
readline = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')
def dijkstra(s, g, N):
# ゴールがない場合はg=-1とする。
def cost(v, m):
return v * N + m
dist = [inf] * N
mindist = [inf] * N
seen = [False] * N
Q = [cost(0, s)]
while Q:
c, m = divmod(heappop(Q), N)
if seen[m]:
continue
seen[m] = True
dist[m] = c
if m == g:
return dist
#------heapをアップデートする。--------
for u, C in G[m]:
if seen[u]:
continue
newdist = dist[m] + C
#------------------------------------
if newdist >= mindist[u]:
continue
mindist[u] = newdist
heappush(Q, cost(newdist, u))
return dist
def f(h, w, c):
return h + N * w + N * N * c
N, M = map(int, readline().split())
temp = [[0] * N for i in range(N)]
for i in range(M):
h, w, c = map(int, readline().split())
h, w = h - 1, w - 1
temp[h][w] = c
G = [[] for i in range(N * N * 2)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(N):
for j in range(N):
for k in range(4):
x = i + dx[k]
y = j + dy[k]
if x < 0 or x > N - 1 or y < 0 or y > N - 1:
continue
G[f(i, j, 0)].append( ( f(x, y, 0), temp[x][y] + 1))
G[f(x, y, 0)].append( ( f(i, j, 0), temp[i][j] + 1))
G[f(i, j, 1)].append( ( f(x, y, 1), temp[x][y] + 1))
G[f(x, y, 1)].append( ( f(i, j, 1), temp[i][j] + 1))
G[f(i, j, 0)].append( ( f(x, y, 1), 1))
G[f(x, y, 0)].append( ( f(i, j, 1), 1))
D = dijkstra(0, f(N - 1, N - 1, 1), N * N * 2)
print(min(D[f(N - 1, N - 1, 0)], D[f(N - 1, N - 1, 1)]))
rlangevin