結果

問題 No.2387 Yokan Factory
ユーザー lam6er
提出日時 2025-03-20 18:45:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,821 ms / 5,000 ms
コード長 1,579 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 200,408 KB
最終ジャッジ日時 2025-03-20 18:45:54
合計ジャッジ時間 15,852 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1
    X = int(input[ptr])
    ptr += 1

    edges = [[] for _ in range(N)]
    max_b = 0

    for _ in range(M):
        u = int(input[ptr]) - 1
        ptr += 1
        v = int(input[ptr]) - 1
        ptr += 1
        a = int(input[ptr])
        ptr += 1
        b = int(input[ptr])
        ptr += 1
        edges[u].append((v, a, b))
        edges[v].append((u, a, b))
        if b > max_b:
            max_b = b

    low = 0
    high = max_b + 1
    ans = -1
    inf = 1 << 60

    while low < high:
        mid = (low + high) // 2
        dist = [inf] * N
        dist[0] = 0
        heap = [(0, 0)]
        found = False
        possible = False

        while heap:
            d, u = heapq.heappop(heap)
            if u == N - 1:
                found = True
                possible = (d <= X)
                break
            if d > dist[u]:
                continue
            for (v, a, b) in edges[u]:
                if b >= mid:
                    new_d = d + a
                    if new_d < dist[v]:
                        dist[v] = new_d
                        heapq.heappush(heap, (new_d, v))

        if not found:
            possible = False
        else:
            possible = possible or (dist[N-1] <= X)

        if possible:
            ans = mid
            low = mid + 1
        else:
            high = mid

    print(ans if ans != -1 else -1)

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