#P15916. [TOPC 2024] Lexicopolis

    ID: 15839 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2024矩阵加速ICPC台湾

[TOPC 2024] Lexicopolis

Description

Welcome to Lexicopolis, the ancient city of legends and treasures. The city is famous for its intricate network of one-way roads. There are nn intersections and mm one-way roads connecting the intersections. People can only travel from intersection uiu_i to intersection viv_i along road ii, and road ii is associated with a magical number wiw_i. A path of length kk from intersection ss to tt is a sequence of roads e1,e2,,eke_1, e_2, \dots, e_k that allows travel from intersection ss to intersection tt. A path is lexicographically smaller than another path if at the first road where they have different magic numbers (not index), the number on the first path is smaller than the number on the second path.

It is rumored that the tourist who figures out the lexicographically smallest path of length kk from intersection ss to intersection tt can receive a gift from the Lexicopolis government. Please write a program to find the lexicographically smallest path of length kk from intersection ss to tt. If it is impossible to travel from intersection ss to tt with exactly kk roads, output 1-1.

Input Format

The first line contains six integers n,m,s,t,x,kn, m, s, t, x, k. nn is the number of intersections. mm is the number of roads. ss is the starting intersection and tt is the ending intersection. xx is a number that will be used for outputting the answer. kk is the length of path. The ii-th of the mm following lines contains three integers uiu_i, viv_i and wiw_i. That means road ii is from intersection uiu_i to intersection viv_i and associated with magic number wiw_i.

Output Format

If there is no path of length kk from intersection ss to tt, output -1. Otherwise, assume such a path exists. Consider the lexicographically smallest path e1,e2,,eke_1, e_2, \dots, e_k, and output i=1kweixki\sum_{i=1}^{k} w_{e_i} x^{k-i} modulo 109+710^9+7, where xx is the number provided as the fifth value in the first line of the input.

3 6 1 3 10 4
1 2 2
2 1 1
1 3 1
3 1 2
2 3 1
3 2 2
1211
3 6 1 3 10 5
1 2 2
2 1 1
1 3 1
3 1 2
2 3 1
3 2 2
12121
6 7 5 6 10 10
1 2 1
2 4 2
3 4 1
4 5 3
5 3 5
4 6 2
6 5 1
121513477
6 7 1 6 123 2
1 2 1000000000
2 4 2
3 4 3
4 5 4
5 3 1
4 6 2
6 5 1
-1

Hint

  • 2n502 \le n \le 50
  • 1mn2n1 \le m \le n^2 - n
  • 1uin1 \le u_i \le n for i{1,2,,m}i \in \{1, 2, \dots, m\}
  • 1vin1 \le v_i \le n for i{1,2,,m}i \in \{1, 2, \dots, m\}
  • 1wi1091 \le w_i \le 10^9 for i{1,2,,m}i \in \{1, 2, \dots, m\}
  • uiviu_i \ne v_i for i{1,2,,m}i \in \{1, 2, \dots, m\}
  • (ui,vi)(uj,vj)(u_i, v_i) \ne (u_j, v_j) for iji \ne j
  • 1sn1 \le s \le n
  • 1tn1 \le t \le n
  • 1k1091 \le k \le 10^9
  • 1x1091 \le x \le 10^9