[Solution] Ela and the Wiring Wizard Codeforces Solution
Ela needs to send a large package from machine to machine through a network of machines. Currently, with the network condition, she complains that the network is too slow and the package can't arrive in time. Luckily, a Wiring Wizard offered her a helping hand.
The network can be represented as an undirected connected graph with nodes, each node representing a machine. wires are used to connect them. Wire is used to connect machines and , and has a weight . The aforementioned large package, if going through wire , will move from machine to machine (or vice versa) in exactly microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index , connecting machine and , and rewire it following these steps:
- Choose one machine that is connected by this wire. Without loss of generality, let's choose .
- Choose a machine that is currently connecting to (including ), call it . Disconnect the wire indexed from , then using it to connect and .
The rewiring of wire will takes microseconds, and the weight of the wire will not change after this operation. After a rewiring, a machine might have some wire connect it with itself. Also, the Wiring Wizard has warned Ela that rewiring might cause temporary disconnections between some machines, but Ela just ignores it anyway. Her mission is to send the large package from machine to machine as fast as possible. Note that the Wizard can use his spell on a wire zero, one, or many times. To make sure the network works seamlessly while transferring the large package, once the package starts transferring from machine , the Wiring Wizard cannot use his spell to move wires around anymore.
Ela wonders, with the help of the Wiring Wizard, what is the least amount of time needed to transfer the large package from machine to .
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line contains and (, ), the number of nodes and number of wires, respectively.
For the next lines, -th line will contains , and (, ) - the indices 2 machines that are connected by the -th edge and the weight of it.
It is guaranteed that the sum of over all test cases does not exceed and the sum of over all test cases does not exceed . The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges.
For each test case, output one integer denotes the least amount of time needed to transfer the large package from machine to .
No comments:
Post a Comment