結果
問題 | No.1 道のショートカット |
ユーザー |
![]() |
提出日時 | 2020-07-25 23:28:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 226 ms / 5,000 ms |
コード長 | 1,336 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 93,440 KB |
最終ジャッジ日時 | 2024-07-20 16:51:18 |
合計ジャッジ時間 | 6,187 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict from heapq import heappop, heappush INF = float('inf') def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): return len(self.graph) def add_edge(self, a, b, w): self.graph[a].append((b, w)) class Dijkstra(object): def __init__(self, graph, s): self.g = graph.graph self.dist = defaultdict(lambda: INF); self.dist[s] = 0 self.Q = [] heappush(self.Q, (self.dist[s], s)) while self.Q: dist_u, u = heappop(self.Q) if self.dist[u] < dist_u: continue for v, w in self.g[u]: alt = dist_u + w if self.dist[v] > alt: self.dist[v] = alt heappush(self.Q, (alt, v)) #処理内容 def main(): N = int(input()) C = int(input()) V = int(input()) S = getlist() T = getlist() Y = getlist() M = getlist() G = Graph() for i in range(V): s, t, cost, w = S[i], T[i], Y[i], M[i] s -= 1; t -= 1 for j in range(301): if j >= cost: G.add_edge(301 * s + j, 301 * t + j - cost, w) D = Dijkstra(G, C) dist = D.dist ans = INF for i in range(301): ans = min(ans, dist[301 * (N - 1) + i]) if ans == INF: print(-1) else: print(ans) if __name__ == '__main__': main()