結果

問題 No.1 道のショートカット
ユーザー rlangevin
提出日時 2023-11-17 00:45:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,669 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,708 KB
最終ジャッジ日時 2024-09-26 05:15:48
合計ジャッジ時間 4,814 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import heappush, heappop
inf = 10 ** 18


def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。

    def cost(v, m):
        return v * N + m

    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    par = [(-1, -1)] * N
    Q = [cost(0, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if m == g:
            return dist, par

        #------heapをアップデートする。--------
        for u, C, ind in G[m]:
            if seen[u]:
                continue
            newdist = dist[m] + C

            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            par[u] = (m, ind)
            heappush(Q, cost(newdist, u))
    return dist, par


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()))
D = []
dic = dict()
for i in range(V):
    S[i], T[i] = S[i] - 1, T[i] - 1
    D.append((Y[i], S[i], T[i], M[i], i))
    dic[i] = Y[i]
    
D.sort()
for _ in range(V):
    G = [[] for i in range(N)]
    for y, s, t, m, ind in D:
        G[s].append((t, m, ind))
    dist, P = dijkstra(0, N - 1, N)
    val = 0
    now = N - 1
    while now != 0:
        if P[now][0] == -1:
            break
        val += dic[P[now][1]]
        now = P[now][0]
    if val <= C and dist[-1] != inf:
        print(dist[-1])
        exit()
    D.pop()
        
print(-1)
0