Night Letter
Time limit1sMemory limit1024 MB
For each query (C, s, e), find the shortest path from s to e that avoids intermediate houses with index at least C.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
In Seonrin Village, there is a tradition of sending fireflies to someone dear every night.
Seonrin Village consists of houses numbered to and bidirectional roads connecting pairs of houses. A firefly may pass through other houses when there is no road directly connecting its starting house and destination, or when a more efficient route exists. House holds drops of dew, and a firefly must drink all the dew of every house it passes through during its journey, excluding the starting house and the destination. Unfortunately, each firefly has a constant , and if it drinks drops of dew or more, it stops flying and falls asleep.
Chanwoo, a resident of Seonrin Village, wondered times about the minimum time for a firefly that cannot drink drops of dew or more to travel from house to house .
Write a program that answers Chanwoo's questions.
Input
The first line gives the number of houses and the number of questions .
Starting from the second line, lines give the road information. Let be the -th number on the -th line. If is a positive integer, it is the time to pass through the road connecting house and house ; if it is , there is no road connecting house and house .
Starting from the next line, lines give integers , , separated by spaces.
This is a question asking for the minimum time for a firefly that cannot drink drops of dew or more to travel from house to house .
Output
Print the answers to the questions over lines, one per line, in order. If reaching the destination is impossible, print .
Constraints
- For all , with : , ,