結果

問題 No.1283 Extra Fee
ユーザー ayaoniayaoni
提出日時 2020-11-06 23:18:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,339 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 86,740 KB
実行使用メモリ 220,300 KB
最終ジャッジ日時 2023-09-29 19:36:08
合計ジャッジ時間 11,907 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,376 KB
testcase_01 AC 91 ms
71,392 KB
testcase_02 AC 94 ms
71,684 KB
testcase_03 AC 93 ms
71,928 KB
testcase_04 AC 92 ms
71,768 KB
testcase_05 AC 94 ms
71,388 KB
testcase_06 AC 99 ms
72,272 KB
testcase_07 AC 92 ms
71,816 KB
testcase_08 AC 101 ms
76,424 KB
testcase_09 AC 96 ms
72,460 KB
testcase_10 AC 97 ms
72,356 KB
testcase_11 AC 475 ms
99,140 KB
testcase_12 AC 496 ms
105,368 KB
testcase_13 AC 470 ms
99,364 KB
testcase_14 AC 1,097 ms
170,848 KB
testcase_15 AC 1,610 ms
220,300 KB
testcase_16 AC 642 ms
109,024 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import collections,heapq
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class Dijkstra:
    def __init__(self):
        self.e = collections.defaultdict(list)

    def add(self, u, v, d, directed=False):
        if directed is False:  # 無向グラフの場合
            self.e[u].append([v, d])
            self.e[v].append([u, d])
        else:  # 有向グラフの場合
            self.e[u].append([v, d])

    def delete(self, u, v):
        self.e[u] = [_ for _ in self.e[u] if _[0] != v]

    def search(self, s):  # sから各頂点までの最短距離
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv, ud in self.e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))
        return d


N,M = MI()
HWC = [tuple(MI()) for _ in range(M)]

Di = Dijkstra()
for i in range(1,N+1):
    for j in range(1,N):
        Di.add((i,j),(i,j+1),1,directed=False)
for i in range(1,N):
    for j in range(1,N+1):
        Di.add((i,j),(i+1,j),1,directed=False)

for h,w,c in HWC:
    if h >= 2:
        Di.delete((h-1,w),(h,w))
        Di.add((h-1,w),(h,w),c+1,directed=True)
    if h <= N-1:
        Di.delete((h+1,w),(h,w))
        Di.add((h+1,w),(h,w),c+1,directed=True)
    if w >= 2:
        Di.delete((h,w-1),(h,w))
        Di.add((h,w-1),(h,w),c+1,directed=True)
    if w <= N-1:
        Di.delete((h,w+1),(h,w))
        Di.add((h,w+1),(h,w),c+1,directed=True)

dist = Di.search((1,1))
dist2 = Di.search((N,N))
x = dist[(N,N)]
ans = dist[(N,N)]
for h,w,c in HWC:
    ans = min(ans,dist[(h,w)]+dist2[(h,w)]-2*c)
print(ans)
0