class Graph: def __init__(self, n, directed=False, decrement=True, edges=[]): self.n = n self.directed = directed self.decrement = decrement self.edges = [[] for _ in range(self.n)] for x, y, cost in edges: self.add_edge(x, y, cost) def add_edge(self, x, y, cost): if self.decrement: x -= 1 y -= 1 self.edges[x].append((y, cost)) if self.directed == False: self.edges[y].append((x, cost)) def dijkstra(self, start=None, INF=10**18): """ 返り値は 0-indexed !!! :param start: スタート地点 :return: スタート地点から各点への距離のリスト 備考: heqpq の比較のための key は第一引数である点に注意( = heappush(heapq, (key,value)) ) """ res = [INF] * self.n if start is None: start=self.decrement start=start-self.decrement res[start] = 0 next_set = [(0, start)] while next_set: dist, p = heappop(next_set) if res[p] < dist: continue """ここで頂点pまでの最短距離が確定。よって、ここを通るのはN回のみ""" for q, cost in self.edges[p]: temp_d = dist + cost if temp_d < res[q]: res[q] = temp_d heappush(next_set, (temp_d, q)) return res def draw(self): """ :return: グラフを可視化 """ import matplotlib.pyplot as plt import networkx as nx if self.directed: G = nx.DiGraph() else: G = nx.Graph() for x in range(self.n): for y, cost in self.edges[x]: G.add_edge(x + self.decrement, y + self.decrement, weight=cost) edge_labels = {(i, j): w['weight'] for i, j, w in G.edges(data=True)} pos = nx.spring_layout(G) nx.draw_networkx(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1') nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) plt.axis("off") plt.show() ######################################################### def example(): global input example = iter( """ 3 3 1 2 1 1 3 1 2 3 3 """ .strip().split("\n")) input = lambda: next(example) def example2(): global input example = iter( """ 6 9 1 2 7 2 3 10 1 3 9 1 6 14 3 6 2 5 6 9 4 5 6 2 4 15 3 4 11 """ .strip().split("\n")) input = lambda: next(example) ######################################################### import sys from heapq import * input = sys.stdin.readline # example2() INF = 10**18 # 大きい数字 N, M, K = map(int, input().split()) data=[] for _ in range(M): x, y, cost = map(int, input().split()) x-=1 y-=1 data.append((x,y,cost)) def binary_search_int(ok,ng,test): """ :param ok: solve(x) = True を必ず満たす点 :param ng: solve(x) = False を必ず満たす点 """ while abs(ok-ng)>1: mid=(ok+ng)//2 if test(mid): ok=mid else: ng=mid return ok ############################################################## def test(k): graph=Graph(N,directed=False,decrement=False) for x,y,cost in data: if cost>k: graph.add_edge(x,y,1) else: graph.add_edge(x,y,0) dist=graph.dijkstra(start=0,INF=INF) return dist[N-1]<=K-1 kmin=binary_search_int(10**6,-1,test) print(kmin)