結果

問題 No.1283 Extra Fee
ユーザー vwxyzvwxyz
提出日時 2023-12-16 07:23:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 2,527 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 622,348 KB
最終ジャッジ日時 2023-12-16 07:23:37
合計ジャッジ時間 10,003 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
17,172 KB
testcase_01 AC 31 ms
10,368 KB
testcase_02 AC 31 ms
10,368 KB
testcase_03 AC 30 ms
10,368 KB
testcase_04 AC 31 ms
10,368 KB
testcase_05 AC 29 ms
10,368 KB
testcase_06 AC 31 ms
10,496 KB
testcase_07 AC 31 ms
10,368 KB
testcase_08 AC 31 ms
10,496 KB
testcase_09 AC 31 ms
10,368 KB
testcase_10 AC 31 ms
10,368 KB
testcase_11 AC 289 ms
45,952 KB
testcase_12 AC 357 ms
54,528 KB
testcase_13 AC 223 ms
39,680 KB
testcase_14 AC 934 ms
140,160 KB
testcase_15 AC 1,512 ms
217,344 KB
testcase_16 AC 334 ms
54,016 KB
testcase_17 MLE -
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
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