#P4197. Mountain Peaks Query in Bytemountains

    ID: 17444 Type: Default 1000ms 256MiB

Mountain Peaks Query in Bytemountains

Mountain Peaks Query in Bytemountains

In Bytemountains, there are \( n \) mountain peaks. Each peak \( i \) has a height \( h_i \). Some pairs of peaks are connected by bidirectional roads. There are \( m \) roads, and each road \( j \) has a difficulty value \( d_j \); a larger difficulty means the road is more challenging.

You are given \( q \) queries. Each query consists of three values \( v \), \( x \), and \( k \). For each query, starting from the peak \( v \) and only using roads with difficulty \( \le x \), you must determine the \( k\)-th highest mountain that is reachable. The heights are ordered in descending order; that is, the highest mountain is the 1st highest. If there are fewer than \( k \) reachable peaks, output \(-1\).

Note: Two peaks are considered connected if there is a path (possibly passing through other peaks) that uses only roads with difficulty \( \le x \).

inputFormat

The input begins with two integers \( n \) and \( m \) representing the number of mountain peaks and the number of roads.

The next line contains \( n \) integers: \( h_1, h_2, ..., h_n \), where \( h_i \) is the height of the \( i \)-th mountain.

Each of the next \( m \) lines contains three integers \( u \), \( v \), and \( d \) indicating that there is a bidirectional road between mountain \( u \) and mountain \( v \) with difficulty \( d \).

The next line contains an integer \( q \) representing the number of queries.

Each of the next \( q \) lines contains three integers \( v \), \( x \), and \( k \) representing a query: starting from peak \( v \), consider only roads with difficulty \( \le x \), and find the \( k\)-th highest reachable mountain.

outputFormat

For each query, output the height of the \( k\)-th highest mountain reachable from the starting peak. If there are fewer than \( k \) reachable peaks, output \(-1\).

sample

5 4
10 20 30 40 50
1 2 5
2 3 3
3 4 8
4 5 2
3
1 5 2
5 2 1
3 3 3
20

40 -1

</p>