結果

問題 No.1473 おでぶなおばけさん
ユーザー moharan627
提出日時 2021-04-09 22:32:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,407 ms / 2,000 ms
コード長 2,399 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 141,440 KB
最終ジャッジ日時 2024-06-25 06:09:57
合計ジャッジ時間 23,778 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LC(): return list(input())
def IC(): return [int(c) for c in input()]
def MI(): return map(int, sys.stdin.readline().split())
INF = float('inf')
MOD = 10**9 + 7
def solve():
    N,M = MI()
    Graph = [[] for i in range(N+1)]
    for m in range(M):
        S,T,D = MI()
        Graph[S].append((T,D))
        Graph[T].append((S, D))
    from collections import deque
    def is_ok(arg):
        Q = deque()
        Seen = [False for i in range(N + 1)]
        Dist = [INF for i in range(N+1)]
        Dist[1] = 0
        s = 1
        Seen[s] = True
        Q.append(s)
        #始点をキューに入れる
        while len(Q) > 0:
            now = Q.popleft()
            #隣接グラフGraphの場合
            for next,weight in Graph[now]:
                if(weight<arg):
                    continue
                if(Seen[next] == False):#未探索ならば
                    Seen[next] = True
                    Q.append(next)
                #始点からの距離とかの計算はここで
                Dist[next] = min(Dist[now]+1,Dist[next])
        if(Dist[N] == INF):
            return False
        else:
            return True
    def meguru_bisect(ng, ok):
        """
        負の無限~(目的値)が条件を満たす場合
        ng…条件を満たさない値の範囲での最小値
        ok…条件を満たす値の範囲での最大値
        """
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok
    W = meguru_bisect(10**9 + 1, 1)
    Q = deque()
    Seen = [False for i in range(N + 1)]
    Dist = [INF for i in range(N + 1)]
    Dist[1] = 0
    s = 1
    Seen[s] = True
    Q.append(s)
    # 始点をキューに入れる
    while len(Q) > 0:
        now = Q.popleft()
        # 隣接グラフGraphの場合
        for next, weight in Graph[now]:
            if (weight < W):
                continue
            if (Seen[next] == False):  # 未探索ならば
                Seen[next] = True
                Q.append(next)
            # 始点からの距離とかの計算はここで
            Dist[next] = min(Dist[now] + 1, Dist[next])
    print(W,Dist[N])
    return
solve()
0