#K56517. Time-Optimized Station Visit
Time-Optimized Station Visit
Time-Optimized Station Visit
You are given a network of stations connected by various routes, each with a travel time. The network is undirected and may have multiple paths between stations. Your task is to determine whether a given sequence of stations can be visited within a specified time limit.
Formally, we have \(n\) stations and \(m\) routes. Each route is represented by a tuple \( (u, v, w) \) indicating that there is an undirected edge between station \(u\) and station \(v\) with travel time \(w\). You are also given an integer \(t\) representing the total allowed time, and a sequence of \(k\) stations that must be visited in the provided order.
For each consecutive pair in the sequence, compute the shortest travel time using any valid path. Let \(T\) be the sum of these shortest travel times. If \(T \le t\), output YES
; otherwise, output NO
.
inputFormat
The input is provided via stdin in the following format:
- An integer \(n\) representing the number of stations.
- An integer \(t\) representing the total allowed time.
- An integer \(m\) representing the number of routes.
- Next \(m\) lines: each line contains three integers \(u\), \(v\) and \(w\) representing a route between stations \(u\) and \(v\) with travel time \(w\).
- An integer \(k\) representing the number of stations in the visit sequence.
- A single line with \(k\) integers representing the station sequence to be visited in order.
outputFormat
Output a single line to stdout containing the string YES
if the sequence of stations can be visited within the given time limit, or NO
otherwise.
7
50
6
1 2 10
2 3 20
2 4 5
4 5 15
4 6 25
6 7 10
4
4 5 7 2
NO
</p>