#C981. Taco District Bridge Traversal
Taco District Bridge Traversal
Taco District Bridge Traversal
You are given n districts and m bridges connecting these districts. Each bridge is characterized by four integers: u, v, t, and l, where:
- u and v denote the two districts connected by the bridge.
- t is the weight class of the bridge.
- l is the maximum weight limit that the bridge can support.
For a resident with weight p who wants to travel from district x to district y, the travel is possible if and only if there exists an integer r (with \(1 \le r \le r_{max}\), where \(r_{max}\) is the maximum weight class available among all bridges) such that, when considering only those bridges with \(t \le r\), there is a path from x to y where every bridge on the path has a weight limit of at least p.
Your task is to determine, for each query, whether the resident is able to travel from the starting district to the destination district under the given conditions.
The mathematical condition for each query can be summarized as follows:
Find if there exists an r satisfying \[ \text{Path exists from } x \text{ to } y \text{ in } G_r \text{, where } G_r = \{ (u,v) \mid t \le r \text{ and } l \ge p \}. \]
inputFormat
The first line of input contains three integers: n (1 \le n), the number of districts; m (1 \le m), the number of bridges; and w (an integer representing the number of weight classes, though it is not directly used in the traversal decision).
The following m lines each contain four integers: u v t l, where u and v (\(1 \le u,v \le n\)) are the connected districts, t is the weight class of the bridge, and l is the maximum weight limit that the bridge can support.
The next line contains an integer q, the number of queries.
Each of the following q lines contains three integers: x y p, denoting a query where a resident of weight p wishes to travel from district x to district y.
All input is given via standard input (stdin).
outputFormat
For each query, output a single line containing YES if the resident can travel from x to y under the conditions described, or NO if not. The output is provided via standard output (stdout).
## sample5 7 3
1 2 1 3
1 3 2 8
2 3 3 5
3 4 1 6
2 5 2 7
4 5 3 10
1 4 2 9
3
1 5 6
2 4 8
3 4 7
YES
NO
YES
</p>