結果
問題 | No.1283 Extra Fee |
ユーザー | c-yan |
提出日時 | 2020-11-12 10:35:10 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,917 bytes |
コンパイル時間 | 213 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 104,972 KB |
最終ジャッジ日時 | 2024-07-22 19:05:28 |
合計ジャッジ時間 | 4,959 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
59,008 KB |
testcase_01 | AC | 40 ms
53,556 KB |
testcase_02 | AC | 40 ms
53,376 KB |
testcase_03 | AC | 41 ms
53,632 KB |
testcase_04 | AC | 40 ms
52,992 KB |
testcase_05 | AC | 41 ms
53,760 KB |
testcase_06 | AC | 48 ms
60,032 KB |
testcase_07 | AC | 40 ms
53,120 KB |
testcase_08 | AC | 45 ms
54,144 KB |
testcase_09 | AC | 43 ms
54,528 KB |
testcase_10 | AC | 43 ms
53,632 KB |
testcase_11 | TLE | - |
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 | -- | - |
ソースコード
from heapq import heappop, heappush INF = 10 ** 18 N, M = map(int, input().split()) c = [[0] * N for _ in range(N)] for _ in range(M): h, w, cc = map(int, input().split()) c[h - 1][w - 1] = cc dp0 = [[INF] * N for _ in range(N)] dp1 = [[INF] * N for _ in range(N)] dp0[0][0] = 0 q = [(0, (0, 0, 0))] while q: cc, (h, w, t) = heappop(q) if t == 0: if dp0[h][w] < cc: continue if h != 0: nh = h - 1 nw = w if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]: dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw] q.append((dp0[nh][nw], (nh, nw, 0))) if dp1[nh][nw] > dp0[h][w] + 1: dp1[nh][nw] = dp0[h][w] + 1 q.append((dp1[nh][nw], (nh, nw, 1))) if h != N - 1: nh = h + 1 nw = w if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]: dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw] q.append((dp0[nh][nw], (nh, nw, 0))) if dp1[nh][nw] > dp0[h][w] + 1: dp1[nh][nw] = dp0[h][w] + 1 q.append((dp1[nh][nw], (nh, nw, 1))) if w != 0: nh = h nw = w - 1 if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]: dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw] q.append((dp0[nh][nw], (nh, nw, 0))) if dp1[nh][nw] > dp0[h][w] + 1: dp1[nh][nw] = dp0[h][w] + 1 q.append((dp1[nh][nw], (nh, nw, 1))) if w != N - 1: nh = h nw = w + 1 if dp0[nh][nw] > dp0[h][w] + 1 + c[nh][nw]: dp0[nh][nw] = dp0[h][w] + 1 + c[nh][nw] q.append((dp0[nh][nw], (nh, nw, 0))) if dp1[nh][nw] > dp0[h][w] + 1: dp1[nh][nw] = dp0[h][w] + 1 q.append((dp1[nh][nw], (nh, nw, 1))) elif t == 1: if dp1[h][w] < cc: continue if h != 0: nh = h - 1 nw = w if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]: dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw] q.append((dp1[nh][nw], (nh, nw, 1))) if h != N - 1: nh = h + 1 nw = w if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]: dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw] q.append((dp1[nh][nw], (nh, nw, 1))) if w != 0: nh = h nw = w - 1 if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]: dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw] q.append((dp1[nh][nw], (nh, nw, 1))) if w != N - 1: nh = h nw = w + 1 if dp1[nh][nw] > dp1[h][w] + 1 + c[nh][nw]: dp1[nh][nw] = dp1[h][w] + 1 + c[nh][nw] q.append((dp1[nh][nw], (nh, nw, 1))) print(min(dp0[N - 1][N - 1], dp1[N - 1][N - 1]))