結果

問題 No.1283 Extra Fee
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2020-11-06 22:12:38
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,671 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 82,584 KB
実行使用メモリ 170,224 KB
最終ジャッジ日時 2024-07-22 12:58:30
合計ジャッジ時間 12,236 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,172 KB
testcase_01 AC 41 ms
52,864 KB
testcase_02 AC 40 ms
52,992 KB
testcase_03 AC 39 ms
53,504 KB
testcase_04 AC 40 ms
53,376 KB
testcase_05 AC 39 ms
53,632 KB
testcase_06 AC 40 ms
53,376 KB
testcase_07 AC 41 ms
53,504 KB
testcase_08 AC 43 ms
53,888 KB
testcase_09 AC 40 ms
53,888 KB
testcase_10 AC 42 ms
53,632 KB
testcase_11 AC 462 ms
86,536 KB
testcase_12 AC 572 ms
89,552 KB
testcase_13 AC 376 ms
84,644 KB
testcase_14 AC 841 ms
98,132 KB
testcase_15 AC 1,152 ms
109,304 KB
testcase_16 AC 475 ms
85,808 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 1,412 ms
141,496 KB
testcase_24 AC 1,479 ms
147,864 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

import heapq
def Dijkstra(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    end_flag = [False] * len(lis)
    end_num = 0
    
    q = [[0,start]]

    while len(q) > 0:

        ncost,now = heapq.heappop(q)

        if end_flag[now]:
            continue

        end_flag[now] = True
        end_num += 1

        if end_num == len(lis):
            break

        for nex,ecost in lis[now]:

            if ret[nex] > ncost + ecost:
                ret[nex] = ncost + ecost
                heapq.heappush(q , [ret[nex] , nex])

    return ret


N,M = map(int,stdin.readline().split())

cost =[ [0] * N for i in range(N)]

for loop in range(M):

    h,w,c = map(int,stdin.readline().split())
    h -= 1
    w -= 1
    cost[h][w] += c

ef = [ [ [float("inf"),float("inf")] for i in range(N)] for j in range(N) ]
d = [ [ [False,False] for i in range(N)] for j in range(N) ]

ef[0][0][0] = 0

q = [(0,0,0,0)]

while len(q) > 0:

    ncost,ni,nj,nk = heapq.heappop(q)
    if ni == nj == N-1:
        break

    if d[ni][nj][nk]:
        continue

    d[ni][nj][nk] = True

    if ni != 0 and ef[ni-1][nj][nk] > ef[ni][nj][nk] + 1 + cost[ni-1][nj]:
        ef[ni-1][nj][nk] = ef[ni][nj][nk] + 1 + cost[ni-1][nj]
        heapq.heappush(q , (ef[ni-1][nj][nk],ni-1,nj,nk))

    if ni != 0 and nk == 0 and ef[ni-1][nj][nk+1] > ef[ni][nj][nk] + 1:
        ef[ni-1][nj][nk+1] = ef[ni][nj][nk] + 1
        heapq.heappush(q , (ef[ni-1][nj][nk+1],ni-1,nj,nk+1))

    if ni != N-1 and ef[ni+1][nj][nk] > ef[ni][nj][nk] + 1 + cost[ni+1][nj]:
        ef[ni+1][nj][nk] = ef[ni][nj][nk] + 1 + cost[ni+1][nj]
        heapq.heappush(q , (ef[ni+1][nj][nk],ni+1,nj,nk))

    if ni != N-1 and nk == 0 and ef[ni+1][nj][nk+1] > ef[ni][nj][nk] + 1:
        ef[ni+1][nj][nk+1] = ef[ni][nj][nk] + 1
        heapq.heappush(q , (ef[ni+1][nj][nk+1],ni+1,nj,nk+1))

    if nj != 0 and ef[ni][nj-1][nk] > ef[ni][nj][nk] + 1 + cost[ni][nj-1]:
        ef[ni][nj-1][nk] = ef[ni][nj][nk] + 1 + cost[ni][nj-1]
        heapq.heappush(q , (ef[ni][nj-1][nk],ni,nj-1,nk))

    if nj != 0 and nk == 0 and ef[ni][nj-1][nk+1] > ef[ni][nj][nk] + 1:
        ef[ni][nj-1][nk+1] = ef[ni][nj][nk] + 1
        heapq.heappush(q , (ef[ni][nj-1][nk+1],ni,nj-1,nk+1))

    if nj != N-1 and ef[ni][nj+1][nk] > ef[ni][nj][nk] + 1 + cost[ni][nj+1]:
        ef[ni][nj+1][nk] = ef[ni][nj][nk] + 1 + cost[ni][nj+1]
        heapq.heappush(q , (ef[ni][nj+1][nk],ni,nj+1,nk))

    if nj != N-1 and nk == 0 and ef[ni][nj+1][nk+1] > ef[ni][nj][nk] + 1:
        ef[ni][nj+1][nk+1] = ef[ni][nj][nk] + 1
        heapq.heappush(q , (ef[ni][nj+1][nk+1],ni,nj+1,nk+1))

print (min(ef[N-1][N-1]))
0