Kingdom Roads

Time limit: 2s Memory limit: 256MB Input: Output:

In the Kingdom of Graphonia, there are NN towns connected by MM bidirectional roads, with each pair of towns connected by at most one road. The royal castle is located in the first town (that is, town 11), and the network is connected, meaning it is possible to travel from the royal castle to any other town using the roads.

To minimize maintenance costs, the king has tasked the royal engineers with closing some roads in the kingdom while ensuring the following requirements are met:

  • The network must stay connected.
  • The royal castle must have exactly KK direct connections to other towns.
  • The total maintenance cost of the remaining roads must be minimized.

Task

Help the royal engineers determine the total maintenance cost of the optimized network. If it is impossible to close some of the roads in the required way, write 1-1.

Input data

The first line contains NN, MM and KK.

The (i+1)(i+1)-th of the following MM lines contains three integers, UiU_i, ViV_i and CiC_i, representing a road between towns UiU_i and ViV_i with a maintenance cost CiC_i.

Output data

You need to write a single line with an integer: the unique integer that solves this task.

Constraints and clarifications

  • 2N100 0002 \le N \le 100 \ 000
  • 1M200 0001 \le M \le 200 \ 000
  • 1Kd11 \le K \le d_1, where d1d_1 is the number of roads from the royal castle to other towns.
  • 1Ui,ViN1 \le U_i, V_i \le N, UiViU_i \neq V_i for each i=0M1i=0\ldots M-1.
  • 0Ci1090 \le C_i \le 10^9 for each i=0M1i=0\ldots M-1.
  • There is at most one edge between any two nodes and the edges form a connected graph.
# Points Constraints
1 0 Examples
2 19 Ci=0C_i = 0 for each ii where Ui,Vi1U_i, V_i \neq 1.
3 23 K=d1K = d_1
4 58 No additional constraints.

Example 1

stdin

5 6 2
1 2 3
1 3 1
1 4 4
2 5 1
3 5 5
4 5 9

stdout

11

Explanation

The first and sixth road of the input must be closed.

Example 2

stdin

4 3 2
1 2 2
1 3 7
1 4 1

stdout

-1

Explanation

There is no way to satisfy the requirements.

Log in or sign up to be able to send submissions!