#P1119. 灾后重建
灾后重建
Description
Given the number of villages in Region B, numbered from to , and the lengths of all bidirectional roads. For each village , you are given the day it is completed . All reconstructions start simultaneously, and village is completed on day , becoming open to traffic on that day. If is , the earthquake did not damage that village and it is open from the beginning. Then there are queries . For each query, answer the length of the shortest path from village to village on day . If there is no path from to using only villages that have been completed by day , or if village or village has not been completed by day , output .
Input Format
- The first line contains two positive integers , the number of villages and the number of roads.
- The second line contains non-negative integers , the completion day of each village, and it is guaranteed that .
- The next lines each contain three non-negative integers with , indicating a road between villages and with length . It is guaranteed that , and for any pair of villages there is at most one road.
- The next line contains a positive integer , the number of queries.
- The next lines each contain three non-negative integers , asking for the shortest path length from village to village on day . It is guaranteed that is non-decreasing across the queries.
Output Format
Output lines. For each query , output the corresponding answer: the length of the shortest path from village to village on day . If on day there is no path from to using only villages that have been completed by day , or if village or village has not been completed by day , output .
4 5
1 2 3 4
0 2 1
2 3 1
3 1 2
2 1 4
0 3 5
4
2 0 2
0 1 2
0 1 3
0 1 4
-1
-1
5
4
Hint
- For of the testdata, .
- For of the testdata, ; among them, for of the testdata, and .
- For of the testdata, .
- For of the testdata, , , , and all integers in the input are at most .
Translated by ChatGPT 5
京公网安备 11011102002149号