import heapq def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 M = int(input[idx]) idx += 1 edges = [[] for _ in range(N+1)] for _ in range(M): a = int(input[idx]) idx += 1 b = int(input[idx]) idx += 1 c = int(input[idx]) idx += 1 edges[a].append((b, c)) edges[b].append((a, c)) T = list(map(int, input[idx:idx+N])) idx += N t_list = [0] * (N + 1) # t_list[i] is T_i for town i for i in range(N): t_list[i + 1] = T[i] # Using a priority queue: (time, town, current_p) heap = [] heapq.heappush(heap, (0, 1, 0)) # dist is a dictionary of dictionaries. dist[town][p] = min_time dist = [dict() for _ in range(N + 1)] dist[1][0] = 0 while heap: current_time, u, p_current = heapq.heappop(heap) if u == N: print(current_time) return # Check if the current recorded time is the smallest if p_current not in dist[u] or dist[u][p_current] < current_time: continue # Calculate new p after eating in town u T_u = t_list[u] p_new = p_current + T_u for v, c in edges[u]: if v == u: continue # Should not happen as per the problem's input # Calculate move time move_time = c // p_new total_time = current_time + T_u + move_time # Check if we can update the state for town v with p_new if v == N: # Update the answer immediately print(total_time) return else: if p_new in dist[v]: if dist[v][p_new] <= total_time: continue # Update if this is a better path dist[v][p_new] = total_time heapq.heappush(heap, (total_time, v, p_new)) # According to the problem statement, it's guaranteed to reach N, so this line is a fallback print(-1) if __name__ == "__main__": main()