#P17130. [ICPC 2025 Shanghai R] Yet another mailbox problem

[ICPC 2025 Shanghai R] Yet another mailbox problem

Problem Description

Yana, Mino, White, and Huzz are best friends.

One day, the coach asked White, Mino, and Huzz to prepare a mock contest. Huzz designed a beautifully search problem whose complexity is exponential with respect to kk. He carefully checked the entire problem statement, except for one detail: he accidentally wrote k5×105k \le 5 \times 10^5 instead of k10k \le 10. Later, this problem appeared in a mock contest. The furious contestant, Yana, came to Huzz, asking how the problem was supposed to be solved. But he seemed to have forgotten something, and the problem statement he provided looked slightly different —

You are given a directed graph with nn vertices and mm edges, where each edge ee is assigned an integer weight w(e)w(e) between 11 and 88.

A path is a sequence of edges (e1,e2,,e)(e_1, e_2, \cdots, e_\ell) such that the endpoint of eie_i is the startpoint of ei+1e_{i+1} for all 1i<1 \le i < \ell. The length of the path is \ell, the number of edges it contains. Note that a path may contain the same edge multiple times.

The weight sequence of the path is the sequence of edge weights [w(e1),w(e2),,w(e)][w(e_1), w(e_2), \cdots, w(e_\ell)]. Paths are compared by lexicographical order of their weight sequences.

Two paths are considered distinct as long as they use different edges, even if they share the same vertex sequence and weight sequence. For example, if both paths (e1,e2)(e_1, e_2) and (e3,e4)(e_3, e_4) have weight sequence [1,2][1, 2], and both traverse vertices 1231 \to 2 \to 3, they are still distinct as long as e1e3e_1 \ne e_3 or e2e4e_2 \ne e_4.

White wants to find the lexicographically smallest kk paths. Since the total output may be too large, you only need to output the length of each path.

Input Format

The 11st line of the input contains 33 integers n,m,kn, m, k (2n5×1052 \le n \le 5 \times 10^5, 1m5×1051 \le m \le 5 \times 10^5, 1k5×1051 \le k \le 5 \times 10^5), representing the number of vertices, the number of edges, and the required number of paths.

Each of the next mm lines contains 33 integers x,y,zx, y, z (1x,yn1 \le x, y \le n, 1z81 \le z \le 8, xyx \ne y), representing a directed edge e=(x,y)e = (x, y) with weight w(e)=zw(e) = z. The given edge set may contain multiple edges.

Output Format

Print kk lines. The ii-th line should contain a single integer, the length of the path whose weight is the ii-th smallest in lexicographical order. If there are fewer than ii paths, output 1-1 instead.

5 5 8
2 1 1
3 1 2
4 1 1
1 5 2
5 2 1
1
1
1
2
3
4
5
6
3 4 10
1 2 1
1 2 1
2 3 2
2 3 3
1
1
2
2
2
2
1
1
-1
-1
6 5 15
1 2 3
2 3 5
3 4 2
3 5 1
5 6 4
1
2
1
1
2
3
4
3
1
1
2
3
2
-1
-1

Hint

For simplicity, let eje_j denote the jj-th input edge.

For the first testcase, the lexicographically smallest 88 paths are:

  • Path (e1)(e_1), weight sequence [1][1].
  • Path (e3)(e_3), weight sequence [1][1].
  • Path (e5)(e_5), weight sequence [1][1].
  • Path (e5,e1)(e_5, e_1), weight sequence [1,1][1, 1].
  • Path (e5,e1,e4)(e_5, e_1, e_4), weight sequence [1,1,2][1, 1, 2].
  • Path (e5,e1,e4,e5)(e_5, e_1, e_4, e_5), weight sequence [1,1,2,1][1, 1, 2, 1].
  • Path (e5,e1,e4,e5,e1)(e_5, e_1, e_4, e_5, e_1), weight sequence [1,1,2,1,1][1, 1, 2, 1, 1].
  • Path (e5,e1,e4,e5,e1,e4)(e_5, e_1, e_4, e_5, e_1, e_4), weight sequence [1,1,2,1,1,2][1, 1, 2, 1, 1, 2].