結果

問題 No.2366 登校
ユーザー flygon
提出日時 2023-06-30 23:28:54
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,524 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 254,464 KB
最終ジャッジ日時 2024-07-08 09:00:14
合計ジャッジ時間 41,474 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23 TLE * 1 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

n,m,k,t = map(int,input().split())
t = min(t, 330)
wonderc = defaultdict()
wonderd = defaultdict()
for i in range(k):
    a,b,c,d = map(int,input().split())
    wonderc[(a-1,b-1)] = c
    wonderd[(a-1,b-1)] = d
#ダイクストラ法
def dijkstra(s): # sがスタート、nが頂点数
    INF = 10**15
    dist = [[INF]*(510) for i in range(n*m)]
    que = [(0,s,170)]
    dist[s][170] = 0
    while que:
        c,crr,tt = heappop(que)
        x,y = crr//m, crr%m
        # print(x,y,tt)
        if c > dist[crr][tt]: continue
        if (x,y) in wonderc.keys():
            if dist[crr][max(0, tt-wonderc[(x,y)]+1)] > dist[crr][tt]+wonderd[(x,y)]:
                dist[crr][max(0, tt-wonderc[(x,y)]+1)] = dist[crr][tt]+wonderd[(x,y)]
                heappush(que,(dist[crr][max(0, tt-wonderc[(x,y)]+1)],crr, max(0, tt-wonderc[(x,y)]+1)))
        for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
            nx,ny = x+dx,y+dy
            if not(0<=nx<n and 0<= ny<m): continue
            nxt = nx*m + ny
            if dist[crr][tt] < dist[nxt][min(510-1,tt+1)]:
                dist[nxt][min(510-1,tt+1)] = dist[crr][tt]
                heappush(que,(dist[nxt][min(510-1,tt+1)],nxt,min(510-1,tt+1)))
    return dist

d =dijkstra(0)
ans = min(d[-1][:t+170+1])

print(ans) if ans != 10**15 else print(-1)
0