This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Hurdles

Time limit1sMemory limit128 MB

Summary
For each of many queries, find the route between two stations that minimizes the tallest hurdle, or report -1 if unreachable.
Level

Medium6 of 10

Topics
Graph, Shortest path, Binary search, Sorting
Solved
No attempts yet

Problem

Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to use as little energy as possible to clear the hurdles.

Jumping over several short hurdles is not very hard for a cow, but a single tall hurdle can be very stressful. Therefore the cows only care about the height of the tallest hurdle they have to jump over.

The practice room has NN stations, labeled 1…N1 \ldots N (1≤N≤3001 \le N \le 300). A set of MM one-way paths connects pairs of stations, and the paths are labeled 1…M1 \ldots M (1≤M≤25,0001 \le M \le 25{,}000). Path ii runs from station SiS_i to station EiE_i and contains exactly one hurdle of height HiH_i (1≤Hi≤1,000,0001 \le H_i \le 1{,}000{,}000). A cow must jump every hurdle on any path it traverses.

The cows have TT tasks to complete (1≤T≤40,0001 \le T \le 40{,}000). Task ii consists of two distinct numbers AiA_i and BiB_i (1≤Ai≤N1 \le A_i \le N, 1≤Bi≤N1 \le B_i \le N), meaning a cow must travel from station AiA_i to station BiB_i over one or more paths along some route. For each task the cows want a route that minimizes the height of the tallest hurdle they jump over while traveling from AiA_i to BiB_i. For every task, determine the route whose tallest hurdle is smallest and report that height.

Input

  • Line 1: Three space-separated integers: NN, MM, and TT
  • Lines 2…M+12 \ldots M+1: Line i+1i+1 contains three space-separated integers: SiS_i, EiE_i, and HiH_i
  • Lines M+2…M+T+1M+2 \ldots M+T+1: Line i+M+1i+M+1 contains two space-separated integers describing task ii: AiA_i and BiB_i

Output

  • Lines 1…T1 \ldots T: Line ii contains the result for task ii: the smallest possible maximum hurdle height needed to travel between the two stations. Output −1-1 if it is impossible to travel between them.

Examples1

  1. Example 1

    Input
    5 6 3
    1 2 12
    3 2 8
    1 3 5
    2 5 3
    3 4 4
    2 4 8
    3 4
    1 2
    5 1
    
    Expected output
    4
    8
    -1