#P1119. 灾后重建

灾后重建

Description

Given the number of villages NN in Region B, numbered from 00 to N1N-1, and the lengths of all MM bidirectional roads. For each village ii, you are given the day it is completed tit_i. All reconstructions start simultaneously, and village ii is completed on day tit_i, becoming open to traffic on that day. If tit_i is 00, the earthquake did not damage that village and it is open from the beginning. Then there are QQ queries (x,y,t)(x,y,t). For each query, answer the length of the shortest path from village xx to village yy on day tt. If there is no path from xx to yy using only villages that have been completed by day tt, or if village xx or village yy has not been completed by day tt, output 1-1.

Input Format

  • The first line contains two positive integers N,MN,M, the number of villages and the number of roads.
  • The second line contains NN non-negative integers t0,t1,,tN1t_0,t_1,\cdots,t_{N-1}, the completion day of each village, and it is guaranteed that t0t1tN1t_0 \le t_1 \le \cdots \le t_{N-1}.
  • The next MM lines each contain three non-negative integers i,j,wi,j,w with w10000w \le 10000, indicating a road between villages ii and jj with length ww. It is guaranteed that iji \ne j, and for any pair of villages there is at most one road.
  • The next line contains a positive integer QQ, the number of queries.
  • The next QQ lines each contain three non-negative integers x,y,tx,y,t, asking for the shortest path length from village xx to village yy on day tt. It is guaranteed that tt is non-decreasing across the queries.

Output Format

Output QQ lines. For each query (x,y,t)(x,y,t), output the corresponding answer: the length of the shortest path from village xx to village yy on day tt. If on day tt there is no path from xx to yy using only villages that have been completed by day tt, or if village xx or village yy has not been completed by day tt, output 1-1.

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 30%30\% of the testdata, N50N \le 50.
  • For 30%30\% of the testdata, ti=0t_i=0; among them, for 20%20\% of the testdata, ti=0t_i=0 and N>50N>50.
  • For 50%50\% of the testdata, Q100Q \le 100.
  • For 100%100\% of the testdata, 1N2001 \le N \le 200, 0MN×(N1)20 \le M \le \dfrac{N\times(N-1)}{2}, 1Q500001 \le Q \le 50000, and all integers in the input are at most 10510^5.

Translated by ChatGPT 5