結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2023-12-16 07:23:27 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,527 bytes |
| コンパイル時間 | 526 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 623,648 KB |
| 最終ジャッジ日時 | 2024-09-27 07:28:40 |
| 合計ジャッジ時間 | 9,753 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 MLE * 1 -- * 12 |
ソースコード
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)
vwxyz