結果

問題 No.1283 Extra Fee
ユーザー vwxyzvwxyz
提出日時 2023-12-16 07:23:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,527 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 598,076 KB
最終ジャッジ日時 2023-12-16 07:24:07
合計ジャッジ時間 32,130 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 41 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 48 ms
61,704 KB
testcase_07 AC 46 ms
53,460 KB
testcase_08 AC 48 ms
61,704 KB
testcase_09 AC 57 ms
55,604 KB
testcase_10 AC 39 ms
53,460 KB
testcase_11 AC 228 ms
106,228 KB
testcase_12 AC 208 ms
113,504 KB
testcase_13 AC 174 ms
100,064 KB
testcase_14 AC 359 ms
162,948 KB
testcase_15 AC 491 ms
217,828 KB
testcase_16 AC 186 ms
111,048 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 1,892 ms
492,296 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import heapq

class Graph:
    def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if graph!=None:
            self.graph=graph
            """
            self.edges=[]
            for i in range(self.V):
                if self.weighted:
                    for j,d in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j,d))
                else:
                    for j in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j))
            """
        else:
            self.edges=edges
            self.graph=[[] for i in range(self.V)]
            if weighted:
                for i,j,d in self.edges:
                    self.graph[i].append((j,d))
                    if not self.directed:
                        self.graph[j].append((i,d))
            else:
                for i,j in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)

    def Dijkstra(self,s,route_restoration=False):
        dist=[self.inf]*self.V
        dist[s]=0
        queue=[(0,s)]
        if route_restoration:
            parents=[None]*self.V
        while queue:
            dx,x=heapq.heappop(queue)
            if dist[x]<dx:
                continue
            for y,dy in self.graph[x]:
                if dist[y]>dx+dy:
                    dist[y]=dx+dy
                    if route_restoration:
                        parents[y]=x
                    heapq.heappush(queue,(dist[y],y))
        if route_restoration:
            return dist,parents
        else:
            return dist

N,M=map(int,readline().split())
C=[[0]*N for h in range(N)]
for m in range(M):
    h,w,c=map(int,readline().split())
    h-=1;w-=1
    C[h][w]=c
edges=[]
for h in range(N):
    for w in range(N):
        for dh,dw in ((0,1),(1,0),(0,-1),(-1,0)):
            hh=h+dh
            ww=w+dw
            if 0<=hh<N and 0<=ww<N:
                edges.append((h*N+w,hh*N+ww,C[hh][ww]+1))
                edges.append((h*N+w+N*N,hh*N+ww+N*N,C[hh][ww]+1))
                edges.append((h*N+w,hh*N+ww+N*N,1))
G=Graph(N*N*2,edges=edges,directed=True,weighted=True)
ans=G.Dijkstra(0)[N*N*2-1]
print(ans)
0