#P3953. [NOIP 2017 提高组] 逛公园

    ID: 1652 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>搜索2017NOIp 提高组记忆化搜索最短路

[NOIP 2017 提高组] 逛公园

Description

Cece loves visiting the park. The park can be regarded as a directed graph with NN vertices and MM edges, without self-loops and without multiple edges. Vertex 11 is the entrance and vertex NN is the exit. Each edge has a non-negative weight representing the time Cece spends to traverse that edge.

Cece visits the park every day. He always enters from vertex 11 and exits at vertex NN.

Cece likes new things. He does not want to take exactly the same route on two different days. He also does not want to spend too much time. If the shortest path length from vertex 11 to vertex NN is dd, then Cece only likes routes with length no more than d+Kd + K.

Cece wants to know how many routes satisfy these conditions. Can you help him?

To avoid large output, report the answer modulo PP.

If there are infinitely many valid routes, output 1-1.

Input Format

The first line contains an integer TT, the number of test cases.

Then follow TT test cases. For each test case:

  • The first line contains four integers N,M,K,PN, M, K, P, separated by a single space.
  • The next MM lines each contain three integers ai,bi,cia_i, b_i, c_i, indicating there is a directed edge from vertex aia_i to vertex bib_i with weight cic_i, with integers separated by a single space.

Output Format

The output contains TT lines, each with one integer representing the answer.

2
5 7 2 10
1 2 1
2 4 0
4 5 2
2 3 2
3 4 1
3 5 2
1 5 3
2 2 0 10
1 2 0
2 1 0
3
-1


Hint

[Sample Explanation 1]

For the first test case, the shortest distance is 33. 15,1245,12351\to 5, 1\to 2\to 4\to 5, 1\to 2\to 3\to 5 are 33 valid paths.

[Testdata and Conventions]

For different test points, the scales of various parameters do not exceed the following.

::cute-table{tuack}

Test point ID TT NN MM KK Any 00-weight edges
11 55 55 1010 00 No
22 10310^3 2×1032\times 10^3
33 5050
44
55
66 Yes
77 10510^5 2×1052\times 10^5 00 No
88 33 5050
99 Yes
1010

For 100%100\% of the testdata, 1P1091 \le P \le 10^9, 1ai,biN1 \le a_i, b_i \le N, 0ci10000 \le c_i \le 1000.

The testdata guarantees that at least one valid route exists.


  • 2019.8.30 Added a set of hack testdata by @skicean.
  • 2022.7.21 Added a set of hack testdata by @djwj233.

Translated by ChatGPT 5