CF2164E.Journey
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are on an undirected connected graph of n vertices and m weighted edges. The edges are indexed from 1 to m. The i-th edge connects vertex ui and vi, and has weight wi. You decided to take a wonderful journey around the graph.
Suppose you are at vertex x. You can do the following operations any number of times:
- Mark an edge connecting x and y, take photos along the edge, and move to y, which costs exactly the edge's weight.
- Transfer to another arbitrary vertex z=x by train. You may choose any path x⇝z (not necessarily simple), and the cost is the weight of the edge with the maximum index on that path. Formally, suppose you choose the path with edge indices e1,e2,…,ek, such that there exists an array of vertices p1,p2,…,pk+1 where x=p1, z=pk+1, and for all i in [1,k], edge ei connects pi and pi+1, the cost is wmaxi=1kei.
You are now at vertex 1, and you need to mark every edge at least once and return to vertex 1. Calculate the minimum cost.
Please note the cost for transferring is not the maximum weight on the path, nor the maximum index itself. If you have any questions, refer to the Note section below.
你位于一个包含 n 个顶点和 m 条带权无向边的连通图上。这些边从 1 到 m 编号。第 i 条边连接顶点 ui 和 vi,权重为 wi。你决定在该图上开启一场美妙的旅程。
假设你当前位于顶点 x,你可以任意次执行以下操作:
- 标记一条连接 x 和 y 的边,沿该边拍摄照片,并移动到 y,花费恰好等于该边的权重。
- 乘坐火车转移到另一个任意顶点 z=x。你可以选择任意一条路径 x⇝z(该路径不一定是简单路径),花费为该路径上边编号最大者所对应边的权重。形式化地,假设你选择的路径包含边编号序列 e1,e2,…,ek,且存在顶点序列 p1,p2,…,pk+1 满足 x=p1、z=pk+1,并且对所有 i∈[1,k],边 ei 连接 pi 和 pi+1,则花费为 wmaxi=1kei。
你当前位于顶点 1,需要至少标记每条边一次,并最终返回顶点 1。请计算最小总花费。
请注意:转移操作的花费既不是路径上的最大边权,也不是最大边编号本身,而是最大边编号所对应边的权重。如有疑问,请参阅下方“注意”部分。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases T (1≤T≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤106, 0≤m≤106).
Then m lines, the i-th line contains three integers ui,vi,wi (1≤ui,vi≤n, 1≤w≤109) — meaning that the edge with index i is between vertex ui and vertex vi with weight wi.
It's guaranteed that the described graph is connected.
Also note that the graph may have self-loops and multiple edges.
It is guaranteed that the sums of n and m over all test cases do not exceed 106 each.
每个测试包含多个测试用例。第一行包含测试用例的数量 T(1≤T≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n≤106,0≤m≤106)。
接下来是 m 行,其中第 i 行包含三个整数 ui,vi,wi(1≤ui,vi≤n,1≤w≤109),表示编号为 i 的边连接顶点 ui 和顶点 vi,其权重为 wi。
保证所描述的图是连通的。
还需注意:该图可能包含自环和重边。
保证所有测试用例中 n 的总和与 m 的总和均不超过 106。
输出格式
For each test case output one integer — the minimum cost.
对于每个测试用例,输出一个整数——最小代价。
输入输出样例
输入#1
5 5 6 2 4 15 2 5 4 1 3 6 2 3 9 1 2 10 3 4 7 4 3 1 2 3 1 3 2 1 4 1 2 3 1 2 1 2 1 3 1 1 4 6 6 2 3 10 1 3 10 5 6 10 6 6 1 4 5 10 3 4 10 5 5 1 2 4 5 1 5 4 3 6 2 4 10 1 4 7
输出#1
58 8 8 71 43
说明/提示
Let uev denote going to vertex v from vertex u by edge e.
In the first test case, one possible solution is:
- Initially, you are at vertex 1.
- Mark edge 3 and move to vertex 3, costs 6.
- Mark edge 6 and move to vertex 4, costs 7.
- Mark edge 1 and move to vertex 2, costs 15.
- Mark edge 4 and move to vertex 3, costs 9.
- Transfer to vertex 5. By choosing path 3641225, we can achieve cost 7, since the maximum index among the edges on the path is 6, and w6=7. Note that we don't care about the maximum weight on the path when using operation 2.
- Mark edge 2 and move to vertex 2, costs 4.
- Mark edge 5 and move to vertex 1, costs 10.
The total cost is 6+7+15+9+7+4+10=58.
In the second test case, one possible solution is:
- Mark edge 1 and move to vertex 2, costs 3.
- Transfer to vertex 3 by 211343123, costs 1. Note the path chosen in operation 2 may not be simple.
- Mark edge 2 and move to vertex 1, costs 2.
- Mark edge 3 and move to vertex 4, costs 1.
- Transfer to vertex 1 by 431, costs 1.
记 uev 表示通过边 e 从顶点 u 到达顶点 v。
在第一个测试用例中,一种可能的解法是:
- 初始时,你位于顶点 1。
- 标记边 3 并移动到顶点 3,花费为 6。
- 标记边 6 并移动到顶点 4,花费为 7。
- 标记边 1 并移动到顶点 2,花费为 15。
- 标记边 4 并移动到顶点 3,花费为 9。
- 转移到顶点 5。选择路径 3641225,可实现花费 7,因为该路径上所有边的最大编号为 6,且 w6=7。注意:在执行操作 2 时,我们不关心路径上的最大边权。
- 标记边 2 并移动到顶点 2,花费为 4。
- 标记边 5 并移动到顶点 1,花费为 10。
总花费为 6+7+15+9+7+4+10=58。
在第二个测试用例中,一种可能的解法是:
- 标记边 1 并移动到顶点 2,花费为 3。
- 通过路径 211343123 转移到顶点 3,花费为 1。注意:操作 2 中所选路径未必是简单路径。
- 标记边 2 并移动到顶点 1,花费为 2。
- 标记边 3 并移动到顶点 4,花费为 1。
- 通过路径 431 转移到顶点 1,花费为 1。
输入解题思路,AI测评打分。不知道怎么写?