CF1737D.Ela and the Wiring Wizard

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

The rewiring of wire ii will takes wiw_i 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 11 to machine nn 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 11, 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 11 to nn.

Ela 需要通过一个机器网络,将一个大型包裹从机器 11 发送至机器 nn。当前网络状况下,她抱怨网络太慢,包裹无法及时到达。幸运的是,一位“布线巫师”主动提出帮助。

该网络可建模为一个含 nn 个节点的无向连通图,每个节点代表一台机器;共有 mm 根导线用于连接这些机器。第 ii 根导线连接机器 uiu_i 和 viv_i,其权重为 wiw_i。若该大型包裹经由第 ii 根导线传输,则它将恰好耗时 wiw_i 微秒,从机器 uiu_i 移动到机器 viv_i(或反向移动)。布线巫师可任意多次施放他的魔法。每次施法时,他将选定索引为 ii 的导线(连接机器 uiu_i 和 viv_i),并按以下步骤重布此导线:

  • 选择该导线所连接的其中一台机器;不失一般性,设其为 viv_i;
  • 再选择一台当前与 viv_i 相连的机器(包括 uiu_i),记作 tit_i;断开导线 ii 与 viv_i 的连接,并用它重新连接 uiu_i 和 tit_i。

对导线 ii 执行一次重布操作需耗时 wiw_i 微秒,且该导线的权重 wiw_i 在操作后保持不变。重布后,某台机器可能通过某根导线与自身相连。此外,布线巫师已警告 Ela:重布操作可能导致某些机器之间出现临时性断连;但 Ela 并不在意这一点。她的目标是尽可能快地将大型包裹从机器 11 传送至机器 nn。注意:巫师可对任意一根导线施法零次、一次或多次。为确保包裹传输过程中网络运行无缝衔接,一旦包裹开始从机器 11 出发传输,布线巫师便不能再施法移动任何导线。

Ela 想知道,在布线巫师的帮助下,将大型包裹从机器 11 传送到机器 nn 所需的最短时间是多少?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). The description of the test cases follows.

The first line contains nn and mm (2≤n≤5002 \le n \le 500, n−1≤m≤250000n - 1 \le m \le 250 000), the number of nodes and number of wires, respectively.

For the next mm lines, ii-th line will contains uiu_i, viv_i and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 1≤wi≤1091 \le w_i \le 10^9) - the indices 2 machines that are connected by the ii-th edge and the weight of it.

It is guaranteed that the sum of nn over all test cases does not exceed 500500 and the sum of mm over all test cases does not exceed 250000250 000. The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \le t \le 100)。随后是各测试用例的描述。

第一行包含 nn 和 mm(2≤n≤5002 \le n \le 500,n−1≤m≤250 000n - 1 \le m \le 250\,000),分别表示节点数和导线数。

接下来的 mm 行中,第 ii 行包含 uiu_i、viv_i 和 wiw_i(1≤ui,vi≤n1 \le u_i, v_i \le n,1≤wi≤1091 \le w_i \le 10^9)——表示第 ii 条边所连接的两个机器的编号及其权重。

保证所有测试用例的 nn 之和不超过 500500,所有测试用例的 mm 之和不超过 250 000250\,000。每个测试用例中的图均保证连通、无自环,但可能包含重边。

输出格式

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

对于每个测试用例,输出一个整数,表示将大包裹从机器 11 传输到机器 nn 所需的最少时间。

输入输出样例

  • 输入#1

    3
    8 9
    1 2 3
    6 4 5
    3 5 6
    6 1 3
    7 4 4
    3 8 4
    2 3 3
    7 8 5
    4 5 2
    4 5
    1 2 1
    2 4 1
    3 4 1
    3 1 1
    1 3 2
    8 8
    4 6 92
    7 1 65
    6 5 43
    6 7 96
    4 3 74
    4 8 54
    7 4 99
    2 5 22

    输出#1

    9
    2
    154

说明/提示

Here is the graph in the first test case in the sample input:

Ela can ask the Wiring Wizard to use his spell on wire with the index of 77, which is connecting machines 22 and 33. Then, since the machine 88 is connected to machine 33, the Wiring Wizard can disconnect wire 77 from machine 33 and connect it to machine 88 in 33 microseconds (weight of wire 33).

After that, the package can be sent from machine 11 to machine 88 in 66 microseconds. Therefore, the answer is 3+6=93 + 6 = 9 microseconds.

Here is the graph in the third test case in the sample input:

以下是样例输入中第一个测试用例的图:

Ela 可请求 Wiring Wizard 对编号为 77 的导线(连接机器 22 和 33)施放法术。随后,由于机器 88 与机器 33 相连,Wiring Wizard 可将导线 77 从机器 33 上断开,并将其连接至机器 88,耗时 33 微秒(即导线 33 的权值)。

此后,包裹可从机器 11 发送至机器 88,耗时 66 微秒。因此,答案为 3+6=93 + 6 = 9 微秒。

以下是样例输入中第三个测试用例的图:

输入解题思路,AI测评打分。不知道怎么写?

首页