E Stands for Efficiency

June Square is a young financial company whose goal is to develop a new, efficient, decentralized payment system written entirely in their favourite programming language JavaScri... OCaml! Obviously, the name of their currency would have to be JScoin.

The work started in 2015 when June Square invested in building NN geographically distributed cryptocurrency storage units with communication lines between them. Since this system is global, each person in the world gets a wallet created in one of these storage units. To transfer a single JScoin between two people, the system finds the shortest path in the communications graph between the storage units holding their respective wallets and transfers JScoin along that path. Moving a single JScoin between two directly connected storage units takes exactly 1 megawatt, thus, transferring a JScoin along a path traversing LL connections takes exactly LL megawatts.

To optimise the energy budget, storage units should be built in distant areas with cheap energy. As a result, creating direct connections between them becomes very complicated and expensive. Therefore, the June Square leadership decided to limit the number of direct connections to each storage unit by RR.

Such limitations mean that the connections had to be designed thoroughly. The analytics department has already predicted the number of JScoins transferred during a year between each pair of storage units. Given that knowledge, design the connections network between the units so that the total predicted energy expenditure over a year will be as small as possible.

Input Data

You are given a zip-archive with 1212 tests: 01, 02, \ldots, 12.

Download inputs (problem-e-inputs.zip)

The first line of each test contains three integers: the number of storage units NN (2N1042 \le N \le 10^4), the number of JScoin transaction predictions MM (1M1061 \le M \le 10^6), and the maximum number of direct connections per unit RR (2R42 \le R \le 4).

MM lines follow. The ii-th of these lines contains three integers sis_i, did_i, qiq_i (1si,diN1 \le s_i, d_i \le N, 1qi1051 \le q_i \le 10^5, sidis_i \neq d_i): two storage unit indices and the predicted number of JScoins to transfer between them. No pair of storage units is listed more than once.

Here is a short description of these tests:

Test NN MM RR
01 28 41 3
02 96 256 3
03 96 348 4
04 121 2322 2
05 100 1431 3
06 100 4506 4
07 3141 314159 2
08 544 1620 3
09 1023 1023 2
10 10000 151677 3
11 10000 1000000 4
12 10000 1000000 3

Output Data

You should submit files 01.out, 02.out, \ldots, 12.out with answers to corresponding inputs. Some of the files may be missing.

Each of these files should be in the following format:

  • The first line should contain the number of direct connections KK.
  • The next KK lines should contain the connections as the indices of connected storage units, separated by whitespace.

Scoring

If any of the following happens in your output:

  • there is a format error;
  • there is a storage unit connected to itself;
  • two storage units are connected more than once;
  • a storage unit has more than RR connections;
  • there is no possibility to pass all required JScoins through the connection graph;

then your output is considered to be incorrect.

Otherwise, let Di,jD_{i,j} be the shortest distance from unit ii to unit jj using the provided connections. The total amount of energy in megawatts (or points) is then computed as follows: i=1MDsi,diqi.\sum_{i=1}^{M} D_{s_i,d_i} \cdot q_i.

Your final score for the test is calculated as follows:

  • If the output is incorrect, it gets 00 points.
  • Otherwise, it will be proportional to the cube of the lowest amount of points among all participants for this test divided by the number of points for your output: 300(best_pointsyour_points)3300 \cdot \left( \frac{\texttt{best\_points}}{\texttt{your\_points}} \right)^3.

Note that the scoreboard will show your best score for each test among all your submissions.

Problem by Jane Street