結果

問題 No.1607 Kth Maximum Card
ユーザー qwewe
提出日時 2025-04-24 12:26:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,698 ms / 3,500 ms
コード長 1,373 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 193,064 KB
最終ジャッジ日時 2025-04-24 12:28:08
合計ジャッジ時間 29,913 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    M = int(input[idx]); idx +=1
    K = int(input[idx]); idx +=1

    adj = [[] for _ in range(N+1)]
    max_c = 0
    for _ in range(M):
        u = int(input[idx]); idx +=1
        v = int(input[idx]); idx +=1
        c = int(input[idx]); idx +=1
        adj[u].append((v, c))
        adj[v].append((u, c))
        if c > max_c:
            max_c = c

    low = 0
    high = max_c
    ans = max_c

    def min_bad_edges(x):
        inf = float('inf')
        dist = [inf] * (N + 1)
        dist[1] = 0
        dq = deque()
        dq.append((0, 1))
        while dq:
            d, u = dq.popleft()
            if d > dist[u]:
                continue
            for v, c in adj[u]:
                new_d = d + (1 if c > x else 0)
                if new_d < dist[v]:
                    dist[v] = new_d
                    if c > x:
                        dq.append((new_d, v))
                    else:
                        dq.appendleft((new_d, v))
        return dist[N]

    while low <= high:
        mid = (low + high) // 2
        mb = min_bad_edges(mid)
        if mb <= K - 1:
            ans = mid
            high = mid - 1
        else:
            low = mid + 1

    print(ans)

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