GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Friday 7 October 2022

[Solution] Ela and the Wiring Wizard Codeforces Solution


D. Ela and the Wiring Wizard
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ela needs to send a large package from machine 1 to machine n 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 n nodes, each node representing a machine. m wires are used to connect them. Wire i is used to connect machines ui and vi, and has a weight wi. The aforementioned large package, if going through wire i, will move from machine ui to machine vi (or vice versa) in exactly wi microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index i, connecting machine ui and vi, and rewire it following these steps:

  • Choose one machine that is connected by this wire. Without loss of generality, let's choose vi.
  • Choose a machine that is currently connecting to vi (including ui), call it ti. Disconnect the wire indexed i from vi, then using it to connect ui and ti.

The rewiring of wire i will takes wi 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 1 to machine n 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 1, 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 1 to n.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t100). The description of the test cases follows.

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

The first line contains n and m (2n500n1m250000), the number of nodes and number of wires, respectively.

For the next m lines, i-th line will contains uivi and wi (1ui,vin1wi109) - the indices 2 machines that are connected by the i-th edge and the weight of it.

It is guaranteed that the sum of n over all test cases does not exceed 500 and the sum of m over all test cases does not exceed 250000. The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges.

Output

For each test case, output one integer denotes the least amount of time needed to transfer the large package from machine 1 to n.

No comments:

Post a Comment