結果

問題 No.1 道のショートカット
ユーザー isee
提出日時 2022-12-03 20:38:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 130 ms / 5,000 ms
コード長 1,254 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 81,968 KB
実行使用メモリ 77,288 KB
最終ジャッジ日時 2024-10-11 00:13:29
合計ジャッジ時間 4,600 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

import heapq

def main():
    # 入力
    N = int(input())  # 街の数
    C = int(input())  # 所持金
    V = int(input())  # 道の数
    S = list(map(int, input().split()))  # 前の街
    T = list(map(int, input().split()))  # 次の街
    Y = list(map(int, input().split()))  # その道の通行料
    M = list(map(int, input().split()))  # その道の長さ
    # 計算・出力
    edges = [[] for _ in range(N)]  # 辺の情報 [次の街, 通行料, 距離]
    for i in range(V):
        edges[S[i]-1].append([T[i]-1, Y[i], M[i]])
    d = [[10**10]*(C+1) for _ in range(N)]
    # d[i][j] := 街 i に 料金 j でたどり着く最短距離
    d[0][0] = 0
    q = [[0, 0, 0]]  # [総距離, 街, 料金]
    while q:
        prevD, prevV, prevC = heapq.heappop(q)
        if d[prevV][prevC] < prevD: continue
        for nextV, cost, dist in edges[prevV]:
            nextD = prevD + dist
            nextC = prevC + cost
            if nextC <= C and nextD < d[nextV][nextC]:
                d[nextV][nextC] = nextD
                heapq.heappush(q, [nextD, nextV, nextC])
    ans = min(d[-1])
    print(ans if ans < 10**10 else -1)

if __name__ == "__main__":
    main()
0