#P8950. [YsOI2022] 道路修建

[YsOI2022] 道路修建

Background

Ysuperman is preparing a template test for the children in his kindergarten. Below is a template problem, and he hopes you can help him verify it.

Problem Description

A place has newly built nn cities, and it is planned to build mm directed roads. For the ii-th road, the start is uiu_i, the end is viv_i, and the construction cost is a positive integer wiw_i.

However, before construction starts, an emergency happens. You must immediately choose some of these roads to build so that people in every city can reach some set of kk cities (that is, people in every city can reach at least one of these kk cities).

You want to know: if these kk cities are chosen uniformly at random among the nn cities, what is the expected minimum construction cost.

To avoid fraction input/output, you only need to output the result modulo 998244353998244353.

Input Format

The first line contains three non-negative integers n,m,kn,m,k, representing the number of cities, the number of planned roads, and the number of cities to be chosen.

The next mm lines each contain three positive integers ui,vi,wiu_i,v_i,w_i, describing a planned directed road.

Output Format

In particular, if there exists a way to choose kk cities such that no matter how you build roads, there will always be some city that cannot reach any of these kk cities, output -1.

Otherwise, output one integer in a single line, representing the answer modulo 998244353998244353.

3 4 1
1 2 1
2 1 2
2 3 3
3 1 4
5
4 6 2
1 2 1
1 3 3
2 3 2
3 4 5
4 1 4
4 2 6
6
8 16 3
5 6 7
7 2 10
4 6 4
5 7 5
8 4 12
1 3 8
2 3 6
4 1 8
1 7 2
8 3 1
2 5 3
6 4 11
7 3 14
3 8 9
8 1 13
6 7 16
160432162
4 1 3
2 4 1
-1

Hint

Sample 1 Explanation

There are three ways to choose the set of cities:

  1. Choose city 11. Then building roads 21,312\to 1,3\to 1 has the minimum cost, which is 2+4=62+4=6.

  2. Choose city 22. Then building roads 12,311\to 2,3\to 1 has the minimum cost, which is 1+4=51+4=5.

  3. Choose city 33. Then building roads 12,231\to 2,2\to 3 has the minimum cost, which is 1+3=41+3=4.

So the expected minimum cost is (6+5+4)/3=5(6+5+4)/3=5.

Sample 2 Explanation

There are 66 ways to choose the set of cities:

  1. Choose cities 1,21,2, minimum cost 99.

  2. Choose cities 1,31,3, minimum cost 66.

  3. Choose cities 1,41,4, minimum cost 77.

  4. Choose cities 2,32,3, minimum cost 55.

  5. Choose cities 2,42,4, minimum cost 66.

  6. Choose cities 3,43,4, minimum cost 33.

So the expected minimum cost is (9+6+7+5+6+3)÷6=6(9+6+7+5+6+3)\div 6=6.

Sample 3 Explanation

It is too small to write here, so here is a diagram:

Sample 4 Explanation

When the set of cities is chosen as 1,2,31,2,3, city 44 cannot reach any of 1,2,31,2,3 no matter what, so the answer is 1-1.

Constraints

For 10%10\% of the testdata, n15n\le 15, m30m\le 30.

For 30%30\% of the testdata, n20n\le 20, m50m\le 50.

For another 5%5\% of the testdata, all wiw_i are equal.

For another 5%5\% of the testdata, k=nk=n.

For another 5%5\% of the testdata, k=n1k=n-1.

For another 10%10\% of the testdata, m=nm=n.

For another 20%20\% of the testdata, k=1k=1.

For 100%100\% of the testdata, 2n1052\le n\le 10^5, 1m2×1051\le m\le 2\times 10^5, 1kn1\le k\le n, 1ui,vin1\le u_i,v_i\le n, 0wi9982443520\le w_i\le 998244352.

Input Format

Output Format

Hint

Translated by ChatGPT 5